What does Genesis 38:9 mean?
Judah's firstborn son Er has died, killed by God for an unnamed wickedness. Since Er died without leaving children, Judah has told his second oldest son, Onan, to take Er's widow Tamar as his wife. His responsibility is to have children with her, raising those offspring on behalf of his brother. This was a strong custom in the region at the time, and it would be part of God's law for the nation of Israel (Deuteronomy 25:5–6). This would ensure that the departed man's line could continue. It would also provide for the woman's care, giving her children to support her as she aged.Onan doesn't like the idea. According to this custom, the children will legally be considered heirs of his dead brother. The time and resources Onan must provide caring for them won't be part of his legacy. Some portion of his future earnings and estate will go to children who are legally and culturally considered someone else's sons.
In response, Onan deliberately avoids conceiving children with Tamar. If that meant avoiding intercourse with her, his choice would merely have been callous. The later, formal version of "levirate marriage" would include a way for the surviving brother to avoid taking on the new wife (Deuteronomy 25:7–10). Instead, Onan takes advantage of both the situation and of Tamar. He uses her for sex but interrupts the act at the very end to prevent conception. This is not a single event, but a pattern: it was Onan's choice "whenever" he had sex with Tamar. For that, God will strike Onan dead (Genesis 38:10).
This sin and its dire consequences inspired the term "Onanism." This word is most often applied to what modern people label as "masturbation." A common assumption is that God punished Onan for self-gratification, or for having sex simply for pleasure. Strictly speaking, however, Onan's sin was how he used Tamar for sex while deliberately avoiding taking the responsibilities which came with being married to her. This verse is explicit: Onan's sin was in how he avoided providing an heir for his brother.