Joseph, her husband, being a righteous man, and not willing to make her a public example, intended to put her away secretly. (Matthew 1:19, WEB)
Joseph was betrothed to Mary. Legally, she was his wife, though they had not yet been officially wed. And then he discovered the unthinkable. She was pregnant.
He knew the child wasn’t his. So what else was he to think? The only explanation was that she had been unfaithful. Of course we know the truth now—we know that the child inside her was conceived by the Holy Spirit—but at this point, Joseph did not know. As far as he knew, she was an adulteress.