Spam is any unwanted email message, usually an advertisement, broadcast to a great number of users. Although it is now used almost exclusively in reference to email, the term originated in Usenet newsgroups. Usenet hosted (and still hosts) a great number of topics or 'groups' to which a user can subscribe. When the same message was sent to a great number of groups, the message was said to be 'spam'. As email became more available and popular, advertisers misused it in a manner similar to Usenet, and the 'spam' term was applied to this more ubiquitous and serious problem.
For Usenet, there is a standard method of measuring whether a message qualifies as spam, called the Breidbart Index. There are two ways to send the same message to multiple newsgroups. A cross-posted message lists multiple groups in its "To:" header; it will be visible to readers of any of those groups, but the server only has to store one copy. A multi-posted message is sent separately to each group, so the server must store many copies; this more expensive for the receivers. The Breidbart index takes account of both factors, plugging the data into a formula and considering the message spam if the result exceeds a threshold value. There have been several versions of the index, each with its own formula and threshold; any of them provides an objective means of identifying spam.
Using the word 'spam' in this way is a reference to a Monty Python sketch. In the sketch, people are gathered in a restaurant (including, inexplicably, a group of Vikings in full 12th-century regalia), every item on the menu includes the canned meat product Spam, and people pointlessly repeat the word 'Spam' over and over. The use of the term in Usenet alluded to this absurd and annoying duplication. Some, but not all, of the early Usenet 'spammers' were in fact unscrupulous advertisers, who had discovered a free advertising channel by ignoring Usenet's conventions.
Generally, spammers also ignore the Terms of Service at their ISP; these almost unanimously forbid spam. Nearly all ISPs will summarily disconnect anyone who is caught sending spam, and many anti-spam activists delight in tracing spammers (mainly by reading the email headers) and reporting them to the ISPs. Spammers use various tactics to avoid this — "throwaway" accounts on free email services, forged headers, "bulletproof" servers that will not disconnect them, and spam-sending botnets. Advanced anti-spam activists do not bother chasing the actual senders, but instead target services spammers use, such as the web-hosting service for "spamvertised" sites, or the "bulletproof" sending services.
Email spam is illegal to broadcast in the US and many other countries. Spam has sparked something of a technology arms race between those who seek to illegally profit from sending spam, and those trying to eliminate it. Spammers harvest, trade and sell collections of email addresses; spam-fighters working at various stages of the internet infrastructure erect spam-filters to detect and eliminate spam. Spammers attempt to defeat those filters by disguising their emails as legitimate by various means, for example, including random bits of text to make each email different.
The term 'spam' is sometimes more loosely used to describe any act of for-profit promotion in a public forum.