One of the skills tested by the various state-level high school English Language Arts exams - previously, in Texas, the TAKS tests; now, the STAAR tests and end-of-course exams - is the ability to identify the main character of a written story or excerpt. I don't think it's too strong to say that if you can't do that, then you probably shouldn't be graduated from high school.
So why has the historical reading of Genesis 38 focused on Onan, of all people? He's in all of three verses of the darn thing! The story has two main characters, and he's not even one of them! The two main characters are Judah and Tamar.
Is it mean of me to suggest that the reason Onan gets so much attention is because one of those main characters is (a) a woman who (b) uses sex to get her way, and (c) not only doesn't get punished for it, she becomes an ancestress of King David, and thus (in Christian myth) of Jesus Christ?
The main actor in the story is Tamar, a Canaanite woman whom Judah selects as the wife of his firstborn son, Er. (Awkward name, that.) Jehovah is offended by something Er does - the transgression is unspecified, and presumably not important to the story - and offs him. Judah then hands off Tamar to his second son, Onan, in the standard Jewish tradition of levirate marriage.
Note that by such tradition, the whole point of this marriage is to get Tamar pregnant with a son who will inherit Er's share of Judah's property. The descendants of Abraham at the time practiced a number of inheritance systems; in the one described in Deuteronomy 21, the father's property would be divided into a number of portions equal to his number of sons + 1, with the extra share going to the eldest son. In other cases, they seem to have practiced strict primogeniture, with the oldest son getting almost everything, or at least all the land. In either case, it's clearly not in Onan's best financial interest for Tamar to have a son who will be considered Er's. (Later on, a child of either gender will fulfill the obligation, but at this point in the Hebrews' history, women aren't allowed to inherit anything except under very unusual circumstances.)
The story even makes it clear that Onan's intent is not merely to have sex without issue, or to spill his semen on the ground because he can - it's to avoid having Tamar produce Er's heir. And it would be perfectly possible for him to achieve this by not sleeping with her at all; both the descendants of Abraham and the Canaanite peoples they lived among were polygynous and had legal prostitution, so he had other outlets. Instead, he practices coitus interruptus with a woman who will essentially become a legal non-entity if she doesn't conceive. Onan's sin clearly isn't masturbation, or simply the 'spilling of seed,' or even non-reproductive sex - it's simple greed in wanting to inherit what is rightfully his brother's share, likely compounded with the (lesser at the time) charge of sexual assault, as Tamar was probably not willing to participate in non-reproductive sex with him, at least not until an heir was gotten.
In other words, Onan's sin is a property sin, and a failure to follow the directions of Jehovah as relayed by Judah, not a sex crime at all. And, given what Tamar does next, it seems like bad reading skills to assume that the seed-spillage itself, as separate from the rest of Onan's bad motives, was what pissed Jehovah off enough to kill him, too.
Anyway, Jehovah having offed two of Tamar's husbands, Judah is understandably reluctant to marry her to his third son, especially since said surviving son is underage at the time, so he sends her back to her father's house, telling her that when the third son, Shelah, is old enough, he'll call for her. He doesn't, of course. In the meantime, Judah's own wife dies. When he heads up to shear his sheep next, Tamar, having figured out that Judah isn't going to marry her off to Shelah and is content to let her rot as a widow without any issue at all (which would make her pretty much destitute - under Hebrew law, she's legally obligated to Shelah and not allowed to marry anyone else, either, although I don't know if the same is true for the Canaanites), decides to take some action. So she puts on a veil, dresses up in some nicer clothes, and waits for him at the town gate.
Judah sees her there, mistakes her for a prostitute (so much for the modesty of the veil protecting women's innocence, there), and offers to sleep with her. She bargains with him over the price, and ends up with his staff, the seal that acts as his signature on business documents, and the cord that holds the seal (some merchants wore them around their wrists, like beads on a bracelet) as collateral for a young kid goat. She then goes back to her father's house and changes back into her widow's mourning. When Judah sends a local friend to deliver the kid (which to his credit he does fairly promptly), the friend finds out that there wasn't a prostitute there, and goes back to Judah, confused. Judah, sensing that something is up, decides that it's safer to let her keep the staff and seal than to go looking for them and reveal that he was fooled by a whore.
Three months later, Tamar is visibly pregnant. Note that this implies that not only did she use her sexual attractiveness to get what was due her, not only did she seduce Judah without showing her face, she also had to have been pretty certain she was fertile when she seduced him. This is a smart cookie we're dealing with here, in other words. Judah assumes she's committed adultery and demands that she be burned alive, as this is apparently what patriarchal cultures do with sexually active widows. In return, she displays the staff and seal, explaining that they belong to the man who impregnated her, and asking that he be identified so he can share in her punishment. Judah admits that she got one over on him and that he was wrong for not marrying her off to Shelah (who does not make a personal appearance anywhere in the story, so we have no idea whether he objected to the idea himself or not). Judah apparently adds Tamar to his household at this point, but we're explicitly told he doesn't sleep with her again, and since he's done so once, he can't give her to Shelah now. Not the best outcome for Tamar, but at least she has a roof over her head and a set of social obligations to be fed and clothed, which she wouldn't have had in her in-between widow/betrothed state. She also has two sons, giving birth to twins named Perez and Zerah, who have obligations to take care of her in her old age and will inherit from Judah as his sons, in addition to the older one, Perez, receiving Er's share as the levirate heir.
Now, we turn to the purpose of the story: why did the J author (or, alternately, Moses, or, even more alternately, Jahveh, who honestly doesn't come off all that well in the story) want us to know this? It's in the tradition of "clever, sexy women who bend the rules" stories in the Old Testament - Ruth, Jael, and Esther come to mind - but it's not very edifying. The answer that we have been given by the historical church is that it's a warning about Onan's behavior, but that seems to fail some basic literacy here, as the story is clearly Not About Him. He's more like the guy who isn't given much characterization and then dies at the end of the first act of a horror movie; there might be some vague moral message attached to his behavior, but at the end of the day, the story is about someone else's acts and choices.
In its own context, the story seems to be here to explain why the later tribe of Judah looks like it does, with three clans named after Shelah, Perez, and Zerah. In the context of later books of the Hebrew Bible, it's part of the descent of David, tracing his lineage back to Judah, who is later in Genesis pronounced to have a "rod of rulership" - perhaps part of the justification for having an Israelite monarchy. And in Christian context, Judah is the son of Israel who inherits the messianic promise - and it passes to Perez as part of his inheritance, in turn. The salvation of the world, then, rests on a woman who disobeys orders, dresses up, and seduces her father-in-law when he fails to follow the inheritance laws. No wonder so many someones along the way have managed to read the story as a condemnation of masturbation; honoring a disobedient woman causes a complete failure of reading comprehension.