The two main ways viruses and spyware get into your computer is either through Cookies (which are automatically downloaded to your computer when you browse the web) and through programs that you download to your computer.
To lower the chance of this happening again:
1) Always try to stick to trusted sites. Sites owned by major corporations (Microsoft, Sunsystems, etc.), government sites (.gov in the U.S.), and other major names (Myspace, etc.) are policed frequently and are less likely to have spyware cookies.
2) Scan anything you download BEFORE you open/run it. Every time you download something, scan it with your antivirus software. This can sometimes find viruses hiding in the program before it can infect your PC.
3) Run Full-System antivirus scans regularly. At least once a week, you should have your antivirus program scan your C: drive.
4) Raise the security settings on your browser (Optional). Go to (for Internet Explorer) Tools>Internet Options>Security and you can raise your security level setting. I would recommend running it at Medium level, and that will help filter out things like unsigned cookies or certificates.
Some people also regularly delete cookies (in U.S. web browsers, go to Tools>Internet Options>Delete Cookies). Most of the time, this isn't necessary, though.