The answer to this lies in the Jewish marriage customs of the time. During this period, there were two distinct parts to the marriage process. The first was betrothal, which was a binding exchange of consent made in the presence of witnesses. Betrothal could only be ended by death or by divorce.
Following the betrothal, the bride remained with her family for a period of several months before moving into her husband’s home, where they lived together as man and wife.
At the time of the Annunciation, Mary was betrothed to Joseph, but they had not yet lived together (and, obviously, had not had sexual relations). The news of her pregnancy, as you can imagine, would have been quite an unpleasant shock to Joseph. When he wishes to “divorce her quietly,” it’s actually a generous decision on his part, considering that the penalty for adultery (which in this case, it is assumed that being true to one another as man and wife would be binding at betrothal) was death by stoning. The angel of the Lord, of course, later tells him the true story of the baby’s conception, and Joseph welcomes Mary into his home.