The story of Onan, son of Judah, occurs in Genesis 38:3-10:
So [Shua, the wife of Judah] conceived and bore a son, and [Judah] called his name Er. She conceived again and bore a son, and she called his name Onan. . . . Then Judah took a wife for Er his firstborn, and her name was Tamar. But Er, Judah's firstborn, was wicked in the sight of the LORD; and the LORD killed him. And Judah said to Onan, "Go in to your brother's wife and marry her, and raise up an heir to your brother." But Onan knew that the heir would not be his; and it came to pass, when he went in to his brother's wife, that he emitted on the ground, lest he should give an heir to his brother. And the thing which he did displeased the LORD; therefore He killed him also.
God slew Onan because the man contemptuously refused to fulfill his familial responsibility under the Old Covenant. This particular practice is called levirate marriage, in which a dead man's closest unmarried male relation (usually a younger brother, as in this case) married the widow to produce an heir for the dead man. This duty is spelled out in Deuteronomy 25:5-10 to preserve tribal inheritance rights (verse 6). Another, happier circumstance of levirate marriage is recorded in the book of Ruth, an event that eventually produced Israel's greatest king, David (Ruth 4:17).
Of course, this ancient national statute is no longer applicable today.
Catholic doctrine uses Onan's story to prohibit the use of birth control. However, this is specious reasoning, based on an ancient and flawed notion that sexual relations between a man and his wife are only for the purpose of producing children. God's anger against Onan had everything to do with his failure to fulfill his covenantal obligation, and nothing to do with his method of birth control. Scripture does not directly contemplate the use of birth control, and thus, aside from abortion, it is a matter of personal preference within what God has revealed as proper Christian living. We can be guided by the principles of personal responsibility—stewardship of one's resources to provide for children (I Timothy 5:8)—and love toward one's mate (Ephesians 5:22-33).