Was Onan really struck dead by God for masturbating? Historically, Onan in Genesis 38 has been accused of masturbation, and for this supposed transgression was struck dead.  The term Onanism (or in German Onanie) is still used to describe this practice.

For example:


Jerome write:


  But I wonder why he the heretic Jovinianus set Judah and Tamar before us for an example, unless perchance even harlots give him pleasure; or Onan, who was slain because he grudged his brother his seed. Does he imagine that we approve of any sexual intercourse except for the procreation of children?

Clement of Alexandria, while not making explicit reference to Onan, similarly reflects an early Christian view of the abhorrence of spilling seed:


  Because of its divine institution for the propagation of man, the seed is not to be vainly ejaculated, nor is it to be damaged, nor is it to be wasted. To have coitus other than to procreate children is to do injury to nature



Was Onan's sin really masturbation?
The story of Judah and Tamar is an interesting one, to say the least.  Judah and Tamar are both explicitly mentioned in Jesus' lineage (Matthew 1 - and Tamar is only one of four women mentioned, all of whom have sordid stories associated with them).  In the end, Tamar is going to trick her father-in-law into getting her pregnant, and then avoids death by proving Judah is the father.  But the question is about Onan, not Judah...

In the story, Tamar is first wed to Er, the eldest of Judah's sons.  Er dies for some unmentioned reason, and Tamar is left without children. Children in that time were important both for the social support for parents in their old age, and for continuance of the family line.  Being childless, the law calls for something called 'levirate marriage' - the next oldest brother is required to marry and give the widow the children.  This is how Judah's second child, Onan, enters the story.


  Then Judah said to Onan, “Sleep with your brother’s wife and fulfill your duty to her as a brother-in-law to raise up offspring for your brother.” But Onan knew that the child would not be his; so whenever he slept with his brother’s wife, he spilled his semen on the ground to keep from providing offspring for his brother. What he did was wicked in the LORD’s sight; so the LORD put him to death also


As you can see, Onan was not masturbating.  If anything, you could accuse Onan of coitus interruptus, but not masturbation.

What Onan was doing, however, was using Tamar for sex, but not giving her the child she deserved.  One could call this theft - it isn't really the sex that is at issue at all. As the text clearly states, Onan did not want his deceased brother to have an heir. That would have negated his claim on the inheritance, and his new found status as eldest brother.

Interestingly, the story continues with this theme of theft - Judah refuses to allow his third son Shelah, to fufill this duty. (I mean, can you blame him? Two sons dead, and now looking at a third?)  Instead of Shelah taking from Tamar, Judah is "stealing" Tamar's due.  In the end, however, Tamar tricks Judah into securing her place in the line of Christ.

Just your typical Sunday School story, I know, but a fun one :)