After learning of Mary's pregnancy, Joseph had to decide how to respond. When he was leaning towards the conclusion to send Mary away secretly (Matthew 1:19), an angel of the Lord appeared to him in a dream. The angel's words revealed to Joseph the way in which he should deal with Mary: do not be afraid to take Mary as your wife (Matthew 1:20). Now it came down to this— would Joseph act according to the words of the angel of the Lord?
Being a righteous man, Joseph obeyed the angel of the Lord:
And Joseph awoke from his sleep and did as the angel of the Lord commanded him, and took Mary as his wife, but kept her a virgin until she gave birth to a Son; and he called His name Jesus. (Matthew 1:24-25)The dream revealed to Joseph that Mary's apparent immorality was actually the coming of Jesus, the man who would save His people from their sins. Thus Joseph, in spite of how he might be seen by the community, took Mary as his wife— for perhaps he considered that any momentary consequences that he might face were nothing in comparison to the great things Mary's Child would bring about in the world.
Joseph was obedient to God. Matthew makes no mention of Joseph quarreling with God or questioning God's wisdom. If there was any question of Joseph's righteousness before the dream, Joseph's obedience after the dream demonstrates his genuine faith. Not only did Joseph obey the angel in taking Mary to be his wife, but Joseph also obeyed the angel in naming the Child Jesus.
It should be pointed out that the information Matthew gives in his gospel concerning the birth of Jesus is limited to the following: she gave birth to a son; and he called His name Jesus. Unlike Luke's gospel, there is no mention of shepherds or angels praising God (however, Matthew's gospel does contain the account of the visit of the magi, something no other gospel writer includes). The birth of this Child, as Matthew pointed out previously, is the fulfillment of Isaiah's prophecy (Matthew 1:23). The birth of Jesus, as Isaiah records, means God with us. The Word— God— had become human flesh, and He made His dwelling among men (John 1:14).
Isaiah's Prophecy: It's Fulfillment (Part 1) - Matthew 1:22-23
Jesus' Purpose: To Save His People From Their Sins (Part 1) - Matthew 1:21
Joseph, Do Not Be Afraid to Take Mary as Your Wife - Matthew 1:20
Joseph's Dilemma - Matthew 1:19