The obvious answer to protect your computer from a virus is to get some anti-virus software.
That being said, it's better to prevent a virus than to have to get rid of one once you get infected. So, here's a few ways to keep your computer healthy and happy. (you don't want an unhappy puter!)
First and foremost, be careful with e-mail attachments. I believe this is the #1 way to get a virus. If you get a file that ends with ".exe" or ".zip" that you're not expecting, don't open it, even if it's from a someone you know (or at least check it with your anti-virus software first).
Many times a person will open an attachment that contains a virus since it appears to come from someone they know. Don't be fooled. Most of these types of viruses propagate themselves by infecting a computer then sending themselves out to everyone in the victim's address book.
When the recipients see the attachment, they believe that their friend sent it, so they run it. Now they're infected and the virus sends itself out to everyone in their e-mail address program. And on and on.
Another way to contract a virus would be to download and run one. Although not nearly as common as getting one e-mailed to you, it can still happen. Make sure when you download from an unknown source you always save to the program to your hard

and check it with your anti-virus software before running it.
Remember 99.99999999 % of the viruses out there can infect your computer ONLY if YOU run them.