The Gospel of Matthew explains that Joseph did not know in advance that Mary was destined to be the mother of the Savior. In Matthew 1:18-25, we read about how Joseph and Mary were betrothed, and Joseph discovered that she was pregnant, a discovery which would surely have been a rather rude shock. (The Jewish marriage ceremony had two parts: betrothal, which was a binding exchange of vows in front of witnesses, followed several months later by the husband taking the wife into his house and living with her as a married couple. If Mary was pregnant following the betrothal but before she and Joseph had lived together as man and wife, Joseph would naturally have assumed that she had been unfaithful to him with another man.)
As the Gospel explains, Joseph was a “righteous man, yet unwilling to expose her to shame” (Matthew 1:19), so he planned to divorce her quietly rather than expose her to the full extent of the law (which would likely have included a death sentence for Mary). Then he receives a visit from the angel of the Lord in a dream, who tells him that Mary’s child was conceived by the Holy Spirit, and commands Joseph to take Mary into his home. From this, then, we learn that Joseph did not know in advance that Mary would be the mother of Jesus, and that he — much like Mary herself — had to be open to the surprising will of God.