Larry Mudd makes some good points. But keep in mind that things are not cut and dried. What one person may call a virus, another person calls a worm, etc.
ITR champion’s statement: "For the record, there is no way that any form of malicious code can infect a computer just by opening an e-mail or viewing a web page. " has been proven patently false hundreds of times. No well written and well configured email client or web browser would do this. Unfortunately, no software of any significance is perfect and MS in particular sets many defaults in their software to the worst possible setting. For the last couple months MS has been trying to figure out how to block a hole that allows downloading of malware just by visiting a webpage. The method is so integral to the OS, that just shutting the hole down makes a lot of other stuff break. They might never be able to produce a patch for it. If you don’t know of dozens of examples of this ocurring, perhaps posting such information is not the best idea.
In general:
A virus needs to “ride along” with another program to infect things. It doesn’t “live” on its own. For old MS-DOS world, command.com, the command line shell, was a frequent target. Every time you loaded command.com, which included startup, the virus attached to it would run, look for other things to infect and so on. If you never run the program that a virus is attached to, the virus will never execute.
A worm propagates itself. The first worm (a good program in this case) was developed at Xerox PARC in the 1970s to find idle machines on PARCs network and do some basic housekeeping. Most of the rapidly spreading stuff on the 'Net in recent years are worms.
The description of a trojan given so far suffices. Keep in mind that it is named after the Trojan Horse.
How do you keep bad stuff off your machine? Don’t let anything come near it unless you are 100% absolutely sure that is completely clean. Do not click on any email attachment. Turn off all email previews. Avoid MS products like IE and Outlook as much as humanly possible. (They’re the #1 targets and they are badly written.) Use a firewall.
If I need a program off the Net: I check into it’s credentials carefully. I Google, get some reviews from respected sites, download only from the official site or its official mirrors. I then run a virus scan on it right then and there. (I keep my virus software up-to-date.) I have been on the 'Net since the 70s, I don’t trust people or companies I don’t know. Neither should you. I actually have two antivirus programs installed. In case a virus takes one out. I use both AdAware and SpyBot Search and Destroy. I pay close attention to the behavior of my machine. If anything seems the least bit different, I start running all the scanning programs.
If I am visiting a web site I am not sure of, I look at the URL before I click. Make sure your browser displays the actual link when the mouse is over it.
E.g., two weeks ago I get an invite to a special social networking forum from a friend. To join I had to click a link. I didn’t do that. I mailed the friend and verified that the email really came from them. I went to the main page of the web site and started snooping (using Opera of course, never IE for a strange site). Two weeks later I decide it’s safe and join. BTW, my friend was invited to join previously by the person who co-ordinated the response to the infamous “Morris Worm” in the 80s. Pretty good creds, but I was still very careful.