In Genesis, Abraham has his first son with Hagar (Genesis 16:11-16), his son Isaac with Sarah (Genesis 21:2-3), and later more sons with Keturah (Genesis 25:1-2)
Yet, when God speaks to Abraham in Genesis chapter 22, and later when the event is recounted in Hebrews 11, Isaac is spoken of as Abraham's "only son":
Genesis 22:2, 12, 16
2 And he said, Take now thy son, thine only son Isaac, whom thou lovest, and get thee into the land of Moriah; and offer him there for a burnt offering upon one of the mountains which I will tell thee of.
12 And he said, Lay not thine hand upon the lad, neither do thou any thing unto him: for now I know that thou fearest God, seeing thou hast not withheld thy son, thine only son from me.
16 And said, By myself have I sworn, saith the LORD, for because thou hast done this thing, and hast not withheld thy son, thine only son:
Hebrews 11:17
17 By faith Abraham, when he was tried, offered up Isaac: and he that had received the promises offered up his only begotten son,
How are these passages to be understood, in light of one another?
To begin, Keturah, and her six sons, are not relevant to the passages in question. Abraham married Keturah after God tested Abraham by telling him to offer his son Isaac in Genesis 22, which is where Isaac is called his "only son".
The reason God calls Isaac Abraham's "only son" is because Isaac, and not Ishmael, was the promised son given to Sarah, which God had ordained would be the line of the Messiah:
Genesis 17:15-21
15 And God said unto Abraham, As for Sarai thy wife, thou shalt not call her name Sarai, but Sarah shall her name be.
16 And I will bless her, and give thee a son also of her: yea, I will bless her, and she shall be a mother of nations; kings of people shall be of her.
17 Then Abraham fell upon his face, and laughed, and said in his heart, Shall a child be born unto him that is an hundred years old? and shall Sarah, that is ninety years old, bear?
18 And Abraham said unto God, O that Ishmael might live before thee!
19 And God said, Sarah thy wife shall bear thee a son indeed; and thou shalt call his name Isaac: and I will establish my covenant with him for an everlasting covenant, and with his seed after him.
20 And as for Ishmael, I have heard thee: Behold, I have blessed him, and will make him fruitful, and will multiply him exceedingly; twelve princes shall he beget, and I will make him a great nation.
21 But my covenant will I establish with Isaac, which Sarah shall bear unto thee at this set time in the next year.
Genesis 21:9-12
9 And Sarah saw the son of Hagar the Egyptian, which she had born unto Abraham, mocking.
10 Wherefore she said unto Abraham, Cast out this bondwoman and her son: for the son of this bondwoman shall not be heir with my son, even with Isaac.
11 And the thing was very grievous in Abraham's sight because of his son.
12 And God said unto Abraham, Let it not be grievous in thy sight because of the lad, and because of thy bondwoman; in all that Sarah hath said unto thee, hearken unto her voice; for in Isaac shall thy seed be called.
In Genesis chapter 21, Hagar and Ishmael are cast out from Abraham, Sarah, and Isaac, and God tells Abraham "in Isaac shall they seed be called". Hence, in the next chapter, God calls Isaac Abraham's "only son" (Genesis 22:2).
This doctrine is expounded in more detail in the New Testament, which references the account when teaching on the fact that Christians are counted for the seed of Abraham:
Romans 9:6-9
6 Not as though the word of God hath taken none effect. For they are not all Israel, which are of Israel:
7 Neither, because they are the seed of Abraham, are they all children: but, In Isaac shall thy seed be called.
8 That is, They which are the children of the flesh, these are not the children of God: but the children of the promise are counted for the seed.
9 For this is the word of promise, At this time will I come, and Sarah shall have a son.
Above, the unbelieving Jews are paralleled with Ishmael, whereas believers are paralleled with Isaac - the children of the promise - and therefore are counted as children of Abraham.
In addition to the above, in the book of Galatians, it's explained that the entire purpose for God allowing the drama with Hagar and Ishmael was to serve as a prophecy of the New Covenant:
Galatians 4:21-31
21 Tell me, ye that desire to be under the law, do ye not hear the law?
22 For it is written, that Abraham had two sons, the one by a bondmaid, the other by a freewoman.
23 But he who was of the bondwoman was born after the flesh; but he of the freewoman was by promise.
24 Which things are an allegory: for these are the two covenants; the one from the mount Sinai, which gendereth to bondage, which is Agar.
25 For this Agar is mount Sinai in Arabia, and answereth to Jerusalem which now is, and is in bondage with her children.
26 But Jerusalem which is above is free, which is the mother of us all.
27 For it is written, Rejoice, thou barren that bearest not; break forth and cry, thou that travailest not: for the desolate hath many more children than she which hath an husband.
28 Now we, brethren, as Isaac was, are the children of promise.
29 But as then he that was born after the flesh persecuted him that was born after the Spirit, even so it is now.
30 Nevertheless what saith the scripture? Cast out the bondwoman and her son: for the son of the bondwoman shall not be heir with the son of the freewoman.
31 So then, brethren, we are not children of the bondwoman, but of the free.
Hagar was Sarah's bondwoman (Genesis 16:1, 21:10-13), and served as an image of the Old Covenant, which is described as "bondage" (Galatians 4:9, 5:1). Sarah is the free woman, an image of the New Covenant.
So, not only is this not a contradiction, but it's an explicit prophecy of things that would not find their fulfillment until thousands of years later.