The story of Mary and Joseph is incredible for a number of reasons. For example, consider how Mary finds out that she is going to have a baby. She's visited by an angel of the Lord who reveals that she will become pregnant through a divine action. She's only about 13 years old and is engaged to Joseph who is probably in his late teens. Joseph learns about the pregnancy but initially he doesn't know about the spiritual intervention.
Therefore, he thinks Mary's been unfaithful to him because he knows he's not been with her. He's left with a tough decision to make. If he accepts Mary's pregnancy, the community will say that he's broken the law by sleeping with her prior to marriage. If he publicly divorces her, he runs the risk that she'll be stoned to death for adultery. Out of compassion, he considers divorcing her privately and not making it a public issue. While he is contemplating his decision, he is also visited by an angel of the Lord who tells Joseph he should go through with the wedding and not be afraid.