"So all this was done that it might be fulfilled which was spoken by the Lord through the prophet, saying: 'Behold, the virgin shall be with child, and bear a Son, and they shall call His name Immanuel,' which is translated, 'God with us.' Then Joseph, being aroused from sleep, did as the angel of the Lord commanded him and took to him his wife, and did not know her till she had brought forth her firstborn Son. And he called His name JESUS" (Matt. 1:22-25).
God had revealed bits and pieces of His plan to the prophets of old, including Isaiah, who is quoted from here regarding a prophecy about a virgin bearing a child. Matthew used the word "fulfilled" 13 times in the book bearing his name, and one of those usages is here. When Matthew employed this word he was affirming that what was taking place was precisely what the Old Testament prophecies stated would take place. In other words, Matthew is showing that God's word is true and that Jesus' miraculous birth had been in the mind of God for a very long time (cf. Rev. 13:8).
Let us take some time to briefly explore the historical context of Isaiah 7:14 here. During the time of the divided kingdom (during the ministry of Isaiah), two kingdoms (Israel and its king, Pekah; and Syria and its king, Rezin) were warring against the kingdom of Judah and its king, Ahaz. Ahaz, showed a definite lack of faith in God, even though God promised him protection if he would just trust Him. Isaiah tried to reassure Ahaz by telling him that the two kings (Rezin & Pekah) are like "smoldering firebrands", and that they are about to "go out;" they will be gone, thus, no longer a threat to Ahaz. Therefore, God says their plan against Ahaz will not come to pass. In order to reassure Ahaz of God's resolve to help him, He allowed Ahaz to ask for any sign he might like. Ahaz refused to "test the Lord," not because he believed, but because he had already made up his mind, and it was not to do what Isaiah and the Lord wanted him to do. In spite of this, Isaiah said the Lord would give the following sign to the "house of David" (Isa. 7:13; instead of to Ahaz personally) - "The virgin shall conceive and bear a Son, and shall call His name Immanuel." Although this prophecy would not be literally fulfilled until Jesus was born, Ahaz would understand (from 7:15,16) this prophecy to mean that within the space of time between when the son was born to the virgin (whenever that son was born), and before he could discern good and evil, the threat from Rezin and Pekah would disappear. Whenever this son was born, he (like other children) would reach the "age of accountability" before reaching his teenage years. Thus, sometime within the next decade or so, the threat from Rezin and Pekah would cease. Immanuel means "God with us" or "God in the flesh." Truly, Jesus fits this description (cf. John 1:1,14) and fulfills this prophecy. Mary was the virgin who bore this special Son, Jesus.
Joseph believed what the angel said and married his betrothed, even though she was pregnant. This action would cause Joseph to be the legally recognized father of Jesus. Joseph is worthy of much honor for his actions here. He is demonstrating a tremendous amount of faith as well as courage. How much ridicule would he endure for his actions? Did he bring shame upon his family? As a side note, the existence of a marriage does not appear to be conditioned upon sexual intercourse (cf. Gen. 2:24).
The wording in Matthew 1:25 implies that he did know his wife sexually at a later time (cf. Matt. 13:55; Mark 6:3), although he kept Mary as a virgin until after Jesus was born.