ANSWER: Mary and Joseph were betrothed to each other at the time of Christ's birth but had not yet consummated their relationship. They were still, however, considered married. The Bible says in this regard, "And the birth of Jesus Christ was as follows: Now His mother Mary had been betrothed to Joseph; but before they came together (had sex), she was found to be with child of the Holy Spirit. (Matthew 1:18 - 19, HBFV throughout)
In Biblical times, a betrothal like the one between Joseph and Mary was considered the same as being married and was a binding agreement between a man and a woman. It was usually accompanied by a partial payment of the full "bride price" paid by the groom (or groom's family) to the bride's father as compensation for the loss of his daughter to marriage (see Genesis 29:18 - 20, Exodus 22:16 - 17, Deuteronomy 22:28 - 29).
Many times this married period or betrothal period lasted a year, but could be longer. During this period the couple lived apart (usually with their parents) until the man came for his wife (see Matthew 25:6) to take her back to his home and consummate the marriage.
Betrothal is something like the modern practice of couples being "engaged" where they promise to marry each other. Instead of a partial payment of the bride price, the man usually buys the woman he has proposed to an engagement ring. There are, of course, major differences between being engaged in today's world versus being betrothed (married) in Bible times.
As stated previously, betrothed couples (like Mary and Joseph) were considered, under God's law (intended to be applicable to all Israelites, and ultimately the world, not just to Jews), to be legally married. If a man decides to end the relationship any time after the betrothal agreement is made (including after consummation), he must pursue legal steps to do so (see Deuteronomy 24:1, also Jeremiah 1:3, 8,).
Additionally, unlike today where sex before the wedding is anything but scandalous, the penalty for having sex during the period of betrothal could be punishable by DEATH (Deuteronomy 22:23 - 24). This is something Joseph and Mary, being righteous, did not indulge in even though they were married.
What if . . .
As Joseph was pondering what he should do in regard to Mary, the "angel of the Lord" appeared to him in a dream and told him to continue his marital relationship with her (Matthew 1:20). What if he had decided to disregard what the angel told him? What would have happened if he had decided to divorce Mary (see Matthew 1:19) and no longer be married to her?
Jesus needed to be the LEGAL descendant of David as well as his biological descendant (as he was through Mary). Inheritance was passed via males in a family, with the firstborn son receiving a double portion (Deuteronomy 21:15 - 17). Christ was prophesied to inherit the throne of David (Luke 1:32), a right he legally received through Joseph (Matthew's lineage of Christ traces his legal lineage to the king). A divorce would have meant he had no legal right to the throne and would have broken prophecy (see Isaiah 9:6 - 7).
Roman law, in difference to the customs of Jews living in the province of Judea, required males (not females) to travel to their ancestral home for census (tax) purposes. This required Joseph to make the journey to Bethlehem, the home of his ancestor David (Luke 1:27, 2:1 - 4). Mary undertook the long journey (roughly 80 miles) because, in part, she would be with her husband who could protect her and help with the birth.
If Mary was divorced it would have made far more sense, given her advanced state of pregnancy, to stay in Nazareth her hometown (Luke 1:26 - 27) where her family lived. This, however, would have broken prophecies stating the Savior would be born in Bethlehem (Micah 5:2, Matthew 2:5 - 6).
Lastly, if Joseph would not have continued to be married to Mary he would have disobeyed the direct revelation from the Eternal that her pregnancy was unique and that it fulfilled righteousness for him to continue the relationship (Matthew 1:20, 24).