Malicious software, like viruses, worms, and Trojan horses, collectively known as malware, can end up on your computer via email attachments and drive-by downloads. It can sneak in when you download free software, especially free anti-malware software. It lurks in seemingly innocuous adware, and takes advantage of vulnerabilities in peer-to-peer programs.
There is really no guaranteed solution to prevent malware from invading your computer, especially since criminals spend a lot of time keeping ahead of the curve to find new and innovative ways to break down your computer’s security defenses.
That said, you can do a lot to guard against malware, such as setting preferences in your web browser to increase security and being cautious about downloading software and clicking links.