Joseph was engaged to marry a girl named Mary, who was expecting a baby in a very unusual way -- as a virgin -- because the Holy Spirit had caused her to conceive Jesus Christ. Mary's pregnancy troubled Joseph so much that he considered ending their engagement (which in his society required a divorce process to cancel an official pledge of marriage).
But God sent an angel to let Joseph what was going on. After hearing the angel's message, Joseph decided to be faithful to God's plan, despite the public humiliation he would have to face from people who thought that he and Mary had conceived the child together before their wedding.
The Bible records in Matthew 1:18-21: "This is how the birth of Jesus the Messiah came about: His mother Mary was pledged to be married to Joseph, but before they came together, she was found to be pregnant through the Holy Spirit. Because Joseph her husband was faithful to the law, and yet did not want to expose her to public disgrace, he had in mind to divorce her quietly. But after he had considered this, an angel of the Lord appeared to him in a dream and said, 'Joseph son of David, do not be afraid to take Mary home as your wife, because what is conceived in her is from the Holy Spirit. She will give birth to a son, and you are to give him the name Jesus, because he will save his people from their sins.'"
God knows what people are thinking before their thoughts ever become words or actions, and this passage shows God sending an angel to speak to Joseph after Joseph simply had divorce "in mind" and "considered" it. The name "Jesus" that the angel tells Joseph to give the baby means "God is salvation." While some people think that the angel who came to Joseph in a dream could have been Gabriel (the archangel who had visited Mary in a vision earlier to inform her that she would serve as Jesus Christ's mother on Earth), the Bible doesn't mention the angel's name.
The Bible passage continues in Mark 1:22-23: "All this took place to fulfill what the Lord had said through the prophet: 'The virgin will conceive and give birth to a son, and they will call him Immanuel' (which means "God with us")."
The verse that Mark 1:23 refers to is Isaiah 7:14 of the Torah. The angel wanted to make it clear to Joseph, a devout Jewish man, that an important prophecy from long ago was being fulfilled through this baby's birth. God knew that Joseph, who loved him and wanted to do what was right, would be motivated to take on the challenge of raising the baby once he knew that the child's birth was fulfilling a prophecy.
The last part of this passage, in Mark 1:23-24, shows how Joseph reacted to the angel's message to him: "When Joseph woke up, he did what the angel of the Lord had commanded him and took Mary home as his wife. But he did not consummate their marriage until she gave birth to a son. And he gave him the name Jesus."
Joseph took care to do everything that the angel had instructed him to do, as well as to honor the purity of what God was accomplishing through Mary. His integrity shows his love for, and faithfulness to, God.