The answer to this is found in Deut 22:20, 21 -
If, however, this accusation is true, and no proof of the young
woman’s virginity can be found, she shall be brought to the door of
her father’s house, and there the men of her city will stone her to
death. For she has committed an outrage in Israel by being promiscuous
in her father’s house. So you must purge the evil from among you.
"Divorce her quietly" meant that Joseph did not intend to have Mary stoned to death for her apparent precociousness. Ellicott observes:
Being a just man. . . .—The glimpse given us into the character of Joseph is one of singular tenderness and beauty. To him, conscious of
being of the house of David, and cherishing Messianic hopes, what he
heard would seem to come as blighting those hopes. He dared not, as a
“righteous” man, take to himself one who seemed thus to have sinned.
But love and pity alike hindered him from pressing the law, which made
death by stoning the punishment of such a sin (Deuteronomy 22:21), or
even from publicly breaking off the marriage on the ground of the
apparent guilt. There remained the alternative, which the growing
frequency of divorce made easy, of availing himself of a “writ of
divorcement,” which did not necessarily specify the ground of
repudiation, except in vague language implying disagreement (Matthew
19:3). Thus the matter would be settled quietly without exposure. The
“bill of divorcement” was as necessary for the betrothed as for those
who were fully man and wife.
Benson goes into more detail:
Matthew 1:19. Joseph her husband, being a just [or righteous] man —
That is, as many understand it, a strict observer of the law, and of
the customs of his ancestors, and therefore not judging it right to
retain her under these circumstances. But the following words, and not
willing to make her a public example, seem manifestly to lead to
another and even an opposite sense of the word here rendered just, or
righteous. Hence some interpret the clause thus: Joseph, being a
good-natured, merciful, and tender- hearted man, was unwilling to go
to the utmost rigour of the law, but chose rather to treat her with as
much lenity as the case allowed. But, Dr. Doddridge very well
observes, it is without any good reason that δικαιος should be here
rendered merciful or good-natured, because, “if we consider the
information which Joseph might have received from persons of such an
extraordinary character as Zachariah and Elizabeth, who would
certainly think themselves obliged to interpose on such an occasion,
and whose story so remarkably carried its own evidence along with it;
besides the intimation the prophecy of Isaiah gave, and the
satisfaction he undoubtedly had in the virtuous character of Mary
herself; we must conclude that he would have acted a very severe and
unrighteous part, had he proceeded to extremities without serious
deliberation; and that putting her away privately would, in these
circumstances, have been the hardest measure which justice would have
suffered him to take. It seems the expression, παραδειγματισαι, here
rendered to make her a public example, “may perhaps refer to that
exemplary punishment which the law inflicted on those who had violated
the faith of their espousals before the marriage was completed. See
Deuteronomy 22:23-24, where it is expressly ordered that a betrothed
virgin, if she lay with another man, should be stoned. We may suppose,
however, that the infamy of a public divorce, though she had not been
stoned, may also be expressed by the same word. But then there was
besides a private kind of divorce, in which no reason was assigned,
and the dowry was not forfeited as in the former case, and by this she
would not have been so much defamed.”