To quote the SAB regarding chapter 38, "This lovely Bible story is seldom read in Sunday School, but it is the basis of many Christian doctrines, including the condemnation of both masturbation and birth control." On-the-mark assessment of this story as a whole, which is a very odd one that only has some of its issues explained through appeal to a better understanding of cultural customs of the time.
Now the first thing that the SAB has issue with in this story is verses 2-4. I really have no idea why. (Steve Wells makes his case in the comments below.) Those verses, along with verse 5, are a simple straightforward setup of the story in which all we see happening is Judah meets a woman and seems to fall in love with her and start a family. What's wrong with that? They have three children, and the eldest of the three marries a woman named Tamar. Up until now, there's not really anything noteworthy of this story.
This eldest son, whose name was Er, does something bad that causes his own death: the Lord kills him. (This may be a figure of speech, but probably not.) Since we don't know what he did to deserve death, I'm not sure we can judge God at all for striking him down. I mean, if you simply don't believe that evil should be punished, or more particularly that you don't believe in capital punishment, then that's more of a personal issue that's part of a larger debate than this passage.
Then comes the weird part, at least to people of more modern sensibilities. Onan, the second brother, is told to go and get his sister-in-law Tamar pregnant. This practice is explained more thoroughly in another part of the Bible later on, but essentially if a woman's husband dies and they have no son, then a near relative is required to impregnate the widow so that the male line is not ended. Telling Onan to do this was something that was supposedly fairly commonplace in that culture and time.
For whatever reason--and despite many people making suggestions such as Onan not wanting to be responsible for a child that would get his older brother's inheritance for instance, we don't really know specifically--Onan decides not to impregnate Tamar, and instead pulls out and "spills his seed". There's a lot of speculation as to what it was that was so particularly wrong with this action on Onan's part. As the SAB notes (and I quoted above) the common beliefs are that it was a matter of the evils of masturbation and/or this particular form of birth control (thus the coinage of the term "onanism" for either practice) or even birth control in general. I tend to think there are a few deeper issues at stake here. The slightly more obvious one is the whole concept of a man impregnating his brother's widow as being so important that it requires an action (sleeping with your sister-in-law) that no doubt would be quite taboo if the circumstances were any different. If it's this important for Onan to fulfill his duties, then refusing to fulfill them is probably a serious offense. On a more subtle note, Tamar is in the genetic line of King David, and therefore also Christ; God may have special reason beyond our understanding to want to see this woman have a child whose father is in the house of Judah. (In my mind, that's what the whole book of Ruth is about.)
So supposedly after Onan died, Tamar should have been married to Shelah, the third brother; but Judah says that he's too young, and Tamar ought to go back and live with her parents until Shelah is old enough. Apparently, though, Judah's just thinking that if he gives his third son to this woman, he's going to die, too. I can't say I fully blame him, it's the sort of thing a father-in-law probably would start to think about.
Eventually enough time passes that Shelah's old enough to get married, and Judah's wife dies, but nobody ever goes to get Tamar back from her parents. Tamar decides that it's time to take matters into her own hands, in a rather unusual manner. She finds out where Judah is going to be the next time he's out of town, and waits for him, dressed up in disguise like a prostitute. It works; Judah sees her, and propositions her for sex.
Now, what Judah is doing, unaware of who the "harlot" really is, is quite in the wrong. He shouldn't be sleeping with some random woman he meets in the road in return for livestock. The story is however quite full of delightful irony. While he is being tempted into doing what is wrong, Tamar is, in a sense, doing what is right, because either Judah or Shelah is supposed to be getting her pregnant so she can have a son as an inheritance for her husband Er. Note that she asks Judah to pledge to her as a deposit, "Thy signet, and thy bracelets, and thy staff," which were symbolic of Judah's authority and inheritance to eventually give to the first-born son of his first-born son. And of course, since Judah is eventually the tribe of Israel out of which the Davidic dynasty comes from, these items are, in a sense, the royal seal and scepter of Israel. Judah hardly seems to pause, though, and hands them over. He gets the sexual gratification he wants, and she gets the child she wants, along with proof that the father is exactly who it should be. (The other irony is the contrast of this story with the immediately following story of Joseph being tempted with sex, and responding very differently.)
I find it interesting that later, when Judah sends a friend to pay off the debt to the "harlot", it echoes some of the supposition that we've seen earlier that the surrounding nations have no moral scruples. Judah's friend seems to have no intent of being delicate about the matter, and simply asks something along the lines of, "Hey guys, where's the whore that used to hang out over there? I owe her something." The men reply that there is no prostitute in the town. Back home, upon hearing the news, Judah decides not to pursue the matter in case it brings him disgrace.
Shortly after, Judah hears that his daughter-in-law (whom he hardly treated as though she existed) was pregnant "by whoredom." Judah gets all indignant and insists that she should be burnt to death. Before this happens, though, Tamar says, "Oh, by the way, the father of the child gave me these," and shows the stuff that Judah gave her. I always love stories like this, of which there are a handful in the Bible, where one person gets all "righteously" angry about somebody else's sin, and is ready to rip them a new one, as they say, and then someone says, "Oh, by the way, what about your involvement?" Judah puts the whole story together and realizes that the only one who really did anything wrong was he himself. Suddenly all that righteous anger goes away, and everyone goes free.
At the birth, it turns out to be twins, so Tamar gets two sons even though Judah never sleeps with her again. Both these sons rise to some prominence in Israel, Pharez of course becoming the next in the royal line to David.