In Matthew 1, Joseph and Mary are described as “betrothed,” a legally binding contract which was something like a “pre-marriage” arrangement. Since Mary is found to be pregnant, she must have been unfaithful. This sad situation almost requires a breaking of the marriage contract, so Joseph decides to divorce her “quietly.”
Joseph does not want to shame her. The verb δειγματίζω is used for the shaming of a woman caught in adultery. It appears in John 8:2 with this sense, and in Dio Chryssostom 47 there is a reference “a Cyprian law, according to which an adulteress had to cut her hair and was subjected to contempt by the community” (BDAG).
This form of the verb does not appear in the LXX, but the compound verb παραδειγματίζω appears 6x. There is little difference in meaning, TDNT 2:31. In Heb 6:6 the compound form is used for shaming Christ by publicly recanting one’s faith. In Col 2:15 uses the verb for the shaming of the “authorities” after when Jesus triumphed over them in the resurrection. In Num 25:4 it describes the public hanging of those who fornicated with the prostitutes from Baal-Peor (compare PsSol 2:12-14, a possible allusion to that story).
The divorce (ἀπολύω) is to be “quiet,” an adverb (λάθρᾳ) often meaning “in secret” or “in private.” In Matt 2:7, for example, Herod summons the wise men “in secret.” It is occasionally used outside of the New Testament with the sense of “not going through proper channels.” It is possible that Joseph, being a poor man, did not feel it necessary to spend the money and time to properly punish her, so he would dissolve the marriage without bringing it before proper authorities who would (perhaps) insist on a shaming of Mary and (undoubtedly) money from Joseph.
Since Joseph was described as a “righteous man,” it is possible that he thought he was obligated by the Law divorce Mary. (John Nolland makes this suggestion and he offers a number of mishnaic sources which indicate that the situation described here may require a divorce. Nolland, Matthew, 95) Numbers 5:11-31 may indicate that if a man discovers his wife in adultery a divorce is required, as well as a public shaming.
While I am not sure that it is correct to connect the “righteousness” of Joseph to keeping a legal tradition requiring the divorce of an adulteress wife, I do think that it is important to read righteousness in a Matthean context rather than importing the Pauline idea into this text. Matthew is not saying that Joseph was “justified” before God, but rather that he was a Jew who was keeping the Law as best that he could. It is possible to read this Greek word as reflecting the same idea as the Hebrew צַדִּיק, “conforming to the laws of God and people” (BDAG).
It is also possible that Joseph did not want to shame himself by declaring to the public that his betrothed wife had been unfaithful. While the text says that it is Mary’s shame that is in mind, Joseph would have a certain level of humiliation when the news became public.
Whatever his motives, Joseph is describe as “doing the right thing” and preserving Mary from a public disgrace and potential execution for adultery.