Commentators explain that Onan's sin was not merely the physical act but was rooted in selfish greed. He knew that under the custom of levirate marriage, the firstborn son would be considered his deceased brother's heir. To prevent this and claim the inheritance for himself, he deliberately refused to fulfill his duty to his family.