The constant tradition of the Catholic Church has been to prohibit artificial contraception. In this we seem to be almost alone today. Yet, historically speaking, all Christian groups opposed contraception altogether until the Anglicans decided in 1930 to allow it for “hard cases” (how sadly familiar that reasoning sounds!).
It’s often thought that the Catholic reasoning behind the prohibition stems from a sort of “anti-sex” or “anti-pleasure” or prudish motivation. The Catholic Church supposedly doesn’t “like” sex, so it requires priests and nuns to be celibate, and seeks to take as much pleasure as possible out of the wondrous divine gift of sexuality. This is untrue, but suffice it for now to say that the relevant biblical arguments have been used by Protestants as well, and stand on their own.
This scriptural basis is perhaps seen most clearly in the passage concerning the sin of Onan (Genesis 38:9-10, RSV):
. . . when he went in to his brother’s wife he spilled the semen on the ground, lest he should give offspring to his brother.  And what he did was displeasing in the sight of the LORD, and he slew him also.
The reasoning often used to overcome the force of the passage is to say that Onan was punished by God (with death) for disobeying the “levirate law,” whereby a brother of a dead husband was to take his sister-in-law as his wife and have children with her (Deuteronomy 25:5-10).
But that can’t apply in this case (or any other) because the law allows the brother to refuse and recommends that the one who does so suffer only public humiliation. Thus we find in Deuteronomy 25:9 that a sister-in-law so refused should “spit in his face,” but there is no mention of any wrath from God , let alone the death penalty.
Moreover, the passage which teaches about the levirate law (Deuteronomy 25:5-10) is directly from God, as part of the covenant and the Law received by Moses on Mt. Sinai, and proclaimed to all of Israel (see Deuteronomy 5:1-5; 29:1, 12). God Himself did not say that the punishment for disobeying the levirate law was death (in the place where it would be expected if it were true).
If refusal alone was not grounds to be killed by God or by capital punishment issued by his fellows, then there must have been something in the way Onan refused which was the cause. This was the “withdrawal method,” a form of contraception (probably the one most used throughout history, because it requires no devices or potions). Therefore, Onan was killed for doing that, which in turn means (we can reasonably conclude!) that God didn’t approve of it.
If a husband died with no children, it was so important to continue his name with offspring that God commanded the man’s brother to take his wife after he died. But Onan tried to separate sex from procreation. He wanted all the pleasure but not the responsibility of perpetuating his brother’s family. He possessed the “contraceptive mentality” which is rampant today, even (sadly) among otherwise traditional, committed Christians.
Fr. Brian Harrison wrote an excellent Internet article (“The Sin of Onan Revisited,” Nov. 1996), in which he examined the passage in great exegetical depth, with incorporation of pertinent cross-texting. He states:
If simple refusal to give legal offspring to his deceased brother were, according to Genesis 38, Onan’s only offence, it seems extremely unlikely that the text would have spelt out the crass physical details of his contraceptive act (cf. v. 9). The delicacy and modesty of devout ancient Hebrews in referring to morally upright sexual activity helps us to see this. As is well-known, Scripture always refers to licit (married) intercourse only in an oblique way: “going in to” one’s wife, (i.e., entering her tent or bedchamber, cf. vv. 8 and 9 in the Genesis text cited above, as well as Gen. 6:4; II Sam. 16:22; I Chron. 23:7) or “knowing” one’s spouse (e.g., Gen. 4:17; Luke 1:34). When the language becomes somewhat more explicit – “lying with” someone, or “uncovering [his/her] nakedness” – the reference is without exception to sinful, shameful sexual acts. And apart from the verse we are considering, the Bible’s only fully explicit mention of a genital act (the voluntary emission of seed) is in a prophetical and allegorical context wherein Israel’s infidelity to Yahweh is being denounced scathingly in terms of the shameless lust of a harlot (Ez. 23:20). . . .
The evil of the contraceptive act stems from its willful, unnatural separation of what God intended to be together. It violates natural law. Onan tried the “middle way” (and the “modern way”) of having sex but willfully separating procreation from it. This was the sin, and it’s why God killed him.
Obviously, God is not immediately punishing or judging in this fashion today (or if so, only in the very rarest of cases), but the point of the Old Testament was to make clear what was right and wrong, and to punish evil swiftly and decisively. Therefore, we learn from this passage that contraception is quite gravely sinful and forbidden; and this general principle of morality didn’t change with the arrival of the new covenant and Christianity.