Onanism is a term that in the past was sometimes used to refer to masturbation. The term comes from the story of Onan in the Bible (Genesis 38:7-9), who was commanded by God to impregnate his late brother's wife (this was the practice of levirate marriage). According to the Bible, whenever he had sexual intercourse with his brother's wife he spilt his semen upon the ground (probably a reference to coitus interruptus); the Bible says that he did this because (under the custom of levirate marriage) the child would not be his, but his brothers. In response to this transgression, God killed Onan.
Since ancient times both Jewish and Christian authors have interpreted this passage as a condemnation of masturbation; but most modern biblical scholars agree that the story in fact has nothing to do with masturbation; Onan's sin was to violate the rules of levirate marriage, an ancient custom no longer practiced by Jews or Christians.