Joseph leaves Mary because he is 'greatly afraid' after what she has just told him.
He's arguing to himself, "If I conceal her sin, I shall be found rebelling against the law of the Lord. But if I expose her to the children of Israel, I am afraid; perhaps what is in her is angelic, and I shall be found betraying innocent blood to the judgment of death. What then shall I do? I will send her away from me secretly." Having decided this, Joseph falls asleep.
He dreams, and an angel appears to him in his dream, telling Joseph that Mary has conceived by the Holy Spirit and that they will name the child Jesus because he would save the people from their sins.
Waking, Joseph rises and glorifies God who has given him this grace, and then he returns home to protect 'the girl'.