Joseph did not know his wife until she gave birth to a Son (Matthew 1:25). Why not?
In Matthew’s account, the conception of Jesus is attributed to the “Holy Spirit” (1:20), and Luke makes it explicit that the one conceived by the Holy Spirit is Himself holy: “the holy thing begotten shall be called the Son of God” (Luke 1:35).
Joseph might have reasoned: Since Mary was inhabited by the Spirit, and by the Holy One conceived by the Spirit, she was, or at least her womb was, holy space. If she is holy space, he cannot have sex with her, since by the rules of Torah sex defiled both the man and the woman (Leviticus 15:18). Having sex with Mary during her pregnancy would have been like a leper or a menstruant entering the temple of God. It would have been like having sex in the temple court itself.
(In the new covenant, we are all inhabited by the Spirit, all “saints.” Does this mean that sex is forbidden? No: Sex no longer defiles, since all have been purified by Jesus’ once-for-all sacrifice.)
If this is the reasoning, it sheds some light on the question of perpetual virginity. Matthew’s phrasing implies that Joseph did have sex with Mary after Jesus was born, and the reasoning above would imply the same. Temples are holy only when the Holy One inhabits them. Once Yahweh abandoned the temple, it was an empty shell for demolition and burning. If Mary was holy because the Holy One lived in her, then His birth exodus from her body would have ended her temporary holiness. She would have reverted to normal “common” status. And Joseph would have known her as his wife.
Joseph refrained from sex with Mary because she was the ark, bearing the glory; but only for nine months.