This version of Joseph is influenced by the Gospel of James, or the Protevangelium, written about 60 years after the Gospel of Matthew, and he was probably created/developed in order to support the pet theory on the part of some early Christians that Mary remained a virgin throughout her life. There are a few bits of scripture that run afoul of this concept, but outside of semantic wording there’s still a really big obstacles on this notion: Jesus’s brothers and sisters.
Centuries later there have been plenty of other explanations proffered, but the easiest one (and the one latched on to by early adherents) was that Joseph had been married before he was engaged to Mary, and that the six siblings of Jesus mentioned in the New Testament –Simon, Joseph Jr, Judas (a different one), James, and two unnamed sisters (do they number two in the gospels? I can't remember, but the plural "sisters" means at least two) – are technically Mary’s stepchildren.
So here’s the story: The priests at the Temple know Mary needs to get married off, but because of the special circumstances surrounding her birth they want to make sure that whatever match occurs is good in the eyes of God. So they summon all of the bachelors in good standing (Joseph, at this point, is a widower), and perform a ceremony to try and discern who God would wish to be Mary’s husband. Joseph goes out of religious obligation, but doesn’t really want to be there. He’s old in his own eyes. He’s had a happy marriage and he misses his wife and he has a bunch of kids, some of whom are already grown, and he doesn’t want to be an old dad with a wife not much older than his youngest child. So he just pays lip service to the ceremony and when all the men are supposed to present their staffs he keeps his at his side. Nothing happens, and the priests do the ceremony again (“we’re not leaving here ‘til God gives us a sign”). One notices that Joseph is basically lip-synching, and they insist he participates. When he does, a dove flies over and lands on his staff.
So Joseph is stuck. He doesn’t want to marry this kid, but the priests insist that it’s his religious obligation.
In some later versions of this story, Mary comes to live at Joseph’s home during the engagement (not unlikely; she wouldn’t have been able to continue living at the temple), learning to run the household and helping to raise (babysit) James, a few years her junior.
Now we get to the Gospel version, which I think finds greater depth via the prequel-reluctant-husband story.
Joseph is engaged to Mary and discovers that she’s pregnant. And he chooses to quietly divorce her (the marriage is pretty much in play once the engagement is solidified; it’s the consummation that finalizes it, held off in this instance because of Mary’s youth) rather than make public her condition or insist on punishment.
Joseph has always been my favorite character in the Bible, and it’s because of this. Even before the story has him learning of the divinity of her pregnancy, he harbors no ill will to her, no desire to reclaim honor by vengeance, no chastisement, nothing of the social barbarity (by our/my standards) that speaks to the time and place and culture from whence he sprung. And from the time I was a kid, I found more nobility in that than near on anything else in the whole book.
The version of Joseph espoused by the early church may have been crafted to explain this calm and measured and merciful side, much at odds with his legalistic predecessors. Joseph is older, so he has the wisdom of experience to recognize and forgive youthful mistakes. He has children, so the usurpation of his bride’s babymaker by another sire doesn’t threaten his bloodline. From a pragmatic fatherly standpoint, his teen biological sons may be at risk of fatal punishment were they suspected of the adulterous union, the most likely paramours given household proximity. And Joseph was a reluctant bridegroom in the first place, so he would likely see Mary’s pregnancy as a blessing, a means by which to remove himself from the engagement through no abandonment of responsibility on his part.
In The Gospel of Matthew Joseph is told in a dream that Mary’s pregnancy is divine, and that God wishes him to wed her.
Though some dogma insists that Joseph was wholeheartedly faithful in his belief of this (there’s even a school of thought championed by St. Thomas Aquinas in which Joseph never suspected any hanky panky and knew that Mary could only have conceived divinely and that the divorce was because he thought himself unworthy, which is such a stretch as to boggle my noggin), but I like reflecting on the perpetual doubt that Joseph must have felt. I prefer my religious figures humanized, and Joseph is at his most admirably human when he discards suspicion, and it doesn’t matter whether that acceptance is rooted in fervent belief or simply the possibility of that belief. That he opts to wed, that he chooses to raise the child as his own, this is the first example we get of the direction that the New Testament will take, this sense of throwing off the legalism of the previous generations for the love and mercy that will be at the heart of Christianity.
My version here has a super fancy staff, much grander than his station would suggest, that I figure he might have made himself as a hobby project, because, you know, carpenter.