You make this sound as if it is a blanket statement, and it was illegal everywhere. This is not true. Not even in the US was it illegal everywhere. In many states, it was completely legal.
It was all part of a "puritan movement" that started in the later part of the 1800's. This movement was also what was responsible for Prohibition in the 1920's. It started with the Comstock Act in 1873, which was a federal law that prohibited the mailing of any article or device that interfered with conception. This was followed by many similar state laws, and some even banning the use of contraceptives.It was all done in an effort to fight "moral evils" like prostitution and obscenity.
Many of these laws stayed on the books, but were not enforced, into the 20th century. There were a few challenges here and there, but nothing that had any standing with the Supreme Court. Then Griswold v. Connecticut came along in 1965 that changed that. This was really the case that started the entire "privacy" rights issue, and the interpretation of the Constitution to imply the right to privacy ("privacy" is not mentioned at all in the Constitution, but many of the Supreme Court justices believe that it is implied through many of the other rights that are guaranteed). The court ruled that MARRIED coupled had a right to use contraceptive. This was later expanded in 1972 (Eisenstadt v. Baird) to include unmarried couples, to the famous 1973 case of Roe v. Wade, and all the way up to the 2003 Lawrence v. Texas case, which ruled that anti-sodomy laws were unconstitutional.