Luke's Gospel tells us that Mary and Joseph were "espoused,' which meant that they were married, but without having yet met the expense of a former marriage ceremony.
Luke tells us that Mary went to visit Elizabeth, and that on her return it was obvious that she was
with child. Now, while Joseph and Mary had privately decided on Mary remaining a virgin, all the people of Nazareth presumed that Joseph was the father of her child.
The Bible describes Joseph as a just man; so as a just man he could not have taken another man's child as his own; but as a just man he could not bring shame on Mary by publicly divorcing her.
What could he do? He was in a find.