Joseph was a man of the law and being engaged to Mary he later found out she was with child. In Joseph’s mind she had lay with another man and knowing what the outcome would be he had it in mind to put Mary away in a private manner that she might not receive the flogging that was due to her.
According to the law, if a woman was bond to man for marriage and was found to break that bond she would be brought before the elders who would administer the flogging in a public place. The intent was to disgrace the woman and Joseph who loved Mary was willing to save her from this public disgrace.
Leviticus 19:20 (KJV And whosoever lies carnally with a woman that is a bondmaid, betrothed to a husband, and not at all redeemed, nor freedom given her; she shall be scourged; they shall not be put to death, because she was not free.”
Matthew 1:19 (KJV) Then Joseph her husband, being a just man, and not willing to make her a public example, was minded to put her away privately.
Psalms 112:4 (KJV) Unto the upright there arises light in the darkness: he is gracious, and full of compassion, and righteous. 5 A good man shows favor, and landed: he will guide his affairs with discretion.
The real picture
In a world dominated by men, it was important for a woman to be married to one husband. If a woman was not able to find a husband, her future during those days would be one of insecurity and loss. Husbands during those days were like retirement investments, one, they provide children who would continue to care for their parents until they pass, so for a woman it was very important for them to mother children.
In the case of Mary and Joseph, the story would have been no different, to be flogged openly would bring disgrace upon Mary and her future; thus preventing her from finding a good husband who would provide for her future. As a result of this, Joseph was willing to keep her situation a secret that is until he was told by the angel to take Mary as his wife because of the prophecy that was to be fulfilled.
If the angel hot not visited Joseph in his dream informing him of the prophecy, Mary sooner or later would have been discovered. If Joseph had put Mary away, it would have been impossible for her to prove she was visited by an angel who created her condition. God had overshadowed Mary and by the power of the Holy Ghost she brought forth a son, but before all this, Mary was facing serious accusations as she faces the elders of her town.
Although the story may seem to suggest that the town’s people were not aware of Mary’s pregnancy, in every way they were aware of what had taken place. In their minds Mary had broken her commitment to Joseph deserving to be flogged openly. In the mind of those who hated her, she was one less competition to worry about, and in the minds of others she was an adulterous woman. The gospel begins with persecution as in Mary’s case, but in the end she was redeemed by her husband Joseph who understood the scripture.