Men are forbidden to spill their semen wastefully. This sin is grave and is punished by G-d, as we find that this was one of the sins of the Generation of the Flood, and one of the reasons that caused the Flood. We also find that Judah's sons Er and Onan were punished by G-d for doing so. Anyone who spills semen wastefully is compared to a murderer.
It is likewise forbidden to watch lewd movies, or to read publications or view Internet sites that include lewd pictures or stories, since all these arouse a man's lustful thoughts. A wise man should be careful in this matter, and he should especially watch over the upbringing of his children, and guard them from involvement with these things. It is the obligation of a society's legal system to ban pornography and punish those who publicize these abominations, since these are things that cause the public to sin.
It is forbidden by Torah Law for a man to deliberately emit semen when it is not being emitted within his woman partner, in midst of their allowed manner of cohabitation, which is vaginal or anal intercourse (this applies unless there is an instance of an overriding medical necessity for him to do so). If a man has the emission in some other way, he is said to be spilling his semen wastefully. This applies to a man or boy who does this to himself by his own action, and to one who allows someone else to do this to him. A man is not held liable for a sin if he unconsciously emits semen during his sleep. However, if he deliberately intends and acts so that this will happen after he falls asleep, it is close to a deliberate transgression.
It is expressly written in the Torah of Moses that it is an action that is evil in G-d's eyes, and it is therefore forbidden in the Noahide Code. Another point regarding this transgression is that it is a destructive act that causes long-term physical and mental (emotional) damage to the person. One who falls into a regular habit of this behavior causes actual damage to himself, and it is forbidden within the Noahide Code to commit self-damage. One who has the ability to marry and have relations with his wife, thereby removing himself from the natural urges to spill his semen, but does not do so and rather spills his semen deliberately, is wicked and has done an evil action similar to that of Er and Onan.
The Sages taught that a man's semen is the strength of his body, his body's life-force, and light of his eyes, and if a man habitually ejaculates overly much, his body and his physical power are weakened, and his life span is shortened. This even includes a man who makes it his habit to engage overindulgently in permitted sexual relations, beyond his expected conjugal duties to his wife - his old age advances upon him prematurely, his strength is weakened, his eyesight diminishes, his mouth and his armpits emit foul odors, and his hair falls out, to name a few of the resulting physical problems and malfunctions that the Sages warned of. With this loss of strength, various pains and medical susceptibilities will come upon him. The wise doctors of earlier generations said that one man in a thousand dies from unrelated illnesses, and the rest die from physical problems that develop from their excessive sexual relations (and masturbation).
The opinion of Tosafot in Tractate Sanhedrin 59b is that a Gentile, who is not commanded to be fruitful and multiply is also not forbidden to spill his semen. This is also the opinion of Rabbeinu Tam in Tosafot Niddah 13a, Kesubot 39a, and Yevamot 12b.
This opinion of Tosafot seems questionable, based on the Talmud in Tractate Niddah 13a, that this was one of the sins that caused the Flood, and on Tractate Yevamot 34b, that Er and Onan were punished by G-d for this act. A possible explanation is that they lived before G-d gave the Torah, and before the Torah was given, Gentile men were still subject to the commandment to be fruitful and multiply (see Rashi on Gen. 1:28). Therefore, they were punished for spilling semen, which violated that command.
However, Ramban Niddah 13 says that the prohibition of spilling semen is independent of the command to be fruitful and multiply (to which Rashba, Ran, and Ritva obviously agree there, since they cite his words). The later Rabbis debated about the reason according to Ramban for this prohibition. It seems that it is deemed to be akin to murder, as the Talmud says (Tractate Niddah there), "those who slaughter children." This is also stated by Mateh Aharon (brought in S'dei Hemed Ma'arehet Zayin, ch. 20). Mishneh Limeleh (Laws of Kings 10:7) explains that according to Ramban, everyone - including even all those who are not commanded to be fruitful and multiply - is forbidden to cause spilling of semen, as it says "all flesh had corrupted its way upon the earth" (Gen. 6:12).
The later Rabbinical authorities ask (Encyclopedia Talmudica vol. 11, "Spilling Semen") where the source for this prohibition is in Torah, as it is not expressly written anywhere in the Hebrew Bible. Therefore, Torat Hesed (Even Ha'Ezer ch. 43) explains that according to Tosafot, spilling semen is a transgression of the command to be fruitful and multiply (and therefore for Gentiles, spilling semen was only a transgression of the pre-Sinai command), whereas Ramban holds that it is a post-Sinai Rabbinical injunction. Based on this, the prohibition for Gentiles would be more lenient, if it existed at all.
However, it appears that the prohibition of spilling semen is written in Torah, but not as an explicit prohibition. This is explained in the responsa P'nei Yehoshua vol. 2, Even Ha'Ezer ch. 44 - that it is forbidden since the Torah writes that the actions of Er and Onan were wicked in G-d's eyes (Gen. 38:7,10). The meaning of his words is that anything the Torah expressly says is "wicked in G-d's eyes" is forbidden for everyone, although it is not written expressly as a prohibition. It is possible that this does not contradict the opinion of Tosafot, who are referring to the practical reason for the prohibition, which would explain that this is not an explicit commandment (to Gentiles, and therefore there is no specific punishment for this act in a court of law), but it is still taken into account that the Torah calls it wicked. It is possible that such a prohibition, though not written expressly in any Torah verse, is even more severe than one that is expressly written, since the Torah clearly says that G-d killed Onan for this. Even though Tosafot says that this was only due to the fact that it was expressly forbidden before the giving of the Torah, it still shows the severe negativity of the action, which logically should indicate that it is forbidden. Ramban, on the other hand, would even say that the prohibition and liability to Divine punishment remained the same afterwards, even though it is not written expressly.
According to all opinions, it is clear that this remains a corrupt act and is counter to acting in the proper way to establish a corrected and settled society, as it says "The world was not created to be empty, but rather to be settled." Since this action is "empty" and corrupt (Levush Even Ha'Ezer ch. 23, Aruh La'ner Niddah 13), it is logical that it is forbidden (the Divine Code by Rabbi Moshe Weiner, Ask Noah International, 2011, p 531-534, 543).