According to Anthony Kennedy, an expert on Medieval theology,
Aquinas, in the Summa contra Gentiles, in a chapter on “the disordered emission of semen” treats both masturbation and contraception as a crime against humanity, second only to homicide. Such a view is natural in the context of a biological belief that only the male gamete provides the active element in conception, so that the sperm is an early stage of the very same individual as eventually comes to birth.
A. Kenney, What I Believe (Continuum 2006), 92-93.
According to the standard Jewish commentary,
The death of Er without a son made Onan subject to the levirate law. Marriage between a man and his brother’s wife is strictly forbidden in the Pentateuchal legislation of Leviticus 18:16 and 20:21. The only exception to the prohibition occurs when the brother dies without a son. According to Deuteronomy 25:5, a man has an obligation to his widowed sister-in-law. This institution is known in Hebrew as yibbum, or “levirate marriage.”
The levirate institution long antedated the Pentateuchal legislation. In fact, it is widely documented in one form or another in several extrabiblical sources. The compendium of laws from the Middle Assyrian Empire (15-14C BCE)…The Hittite laws (14th-13C BCE)…A contract from the town of Nuzi from the middle centuries of the second millennium BCE…[Thus], she would be assured of livelihood and protection. The surviving brother became a surrogate for the deceased husband who posthumously gained a child, socially acknowledged to be his progeny and heir.
The callous refusal of Onan to perpetuate the line of his brother may have been due to a lack of sense of duty to the dead. An even more powerful motivation would have been the fact that with the death of the first-born, Onan inherits one-half of his father’s estate. However, should he provide an heir to his brother, his portion would be diminished.
The text does not make clear specifically why Onan incurs divine wrath. The development of the narrative favors the explanation that it is due to the evasion of his obligation to his dead brother rather than because of the manner in which he acts. By frustrating the purpose of the levirate institution, Onan has placed his sexual relationship with his sister-in-law in the category of incest—a capital offense. The unusual emphasis given to the particular socio-legal background of the story clearly shows that the point at issue is the levirate obligation and not the general topic of birth control.
The JPS Torah Commentary: Genesis (JPS 1989), 266-67.
According to the New Catholic Encyclopedia,
In common usage often taken to mean improperly completed intercourse or even masturbation. The word is taken from the story of Onan in the Book of Genesis…This was in accordance with the custom of Levirate marriage…Popular usage of the term onanism is based on the assumption that the evil for which the Lord took Onan’s life was his unchastity. This, however, is by no means clear from the text, in which his refusal to conform to the prescribed marriage custom can be seen as the wickedness that brought vengeance upon him. Consequently, no certain argument can be based upon this text to prove the sinful character of either improperly completed intercourse or masturbation. Evidence for this must be sought elsewhere.
New Catholic Encyclopedia (CUA, 2nd ed., 2003), 10:600a.