First off, always always install an Antivirus / Antispyware solution on your computer. People have different opinions about what works best. They all have advantages and disadvantes to them. Do a little research and pick one. Some malware will always be able to get through. That is why it is especially important you heed the rest of this post.
Second, be aware of what you are downloading off the internet, which web pages you are visiting, and what you are plugging into your computer. Only download files from trusted sites. Sites like download.com, sourceforge.net and filehippo.com have a reputation for containing safe, malware free downloads. Remember that just visiting a website can infect your computer with something bad. This is because websites run scripts and sometimes these scripts do malicious things. There are a few good ways to protect yourself from these. If you are using Firefox you download an extension called 'noscript'. 'Noscript' blocks all scripts from running unless manually authorized by the computer user. However, this can be a bit difficult to get the hang of for more novice computer users. Another thing you can do is use Google Chrome. Google Chrome has what is called a sandbox built into it. Anything malicious is contained within the sandbox. After Google Chrome is closed, it's like the bad script never ran. Software such as sandboxie can also be used with Internet Explorer to the same effect. Lastly, Windows Vista and 7 allow for Internet Explorer to run in Protected Mode. Protected Mode prevents bad-doers from accessing more privileged parts of your operating systems functionality. Finally don't ever insert a Floppy, CD, DVD, USB, SD drive, etc into your computer unless you know what's been written on it or trust the person giving it to you. USB drives can especially be dangerous because plugging one in to an infected computer can infect the key, which in turn can effect other computers it is plugged into.
If you follow this advice and somehow still end up getting a virus or piece of spyware, it's generally best to wipe the computer clean and restore from backup. See our post about a building a comprehensive backup solution to learn more about different backup strategies.