I don't know how it happens, I've heard people put them in ads that they sell to unknowing websites, so it could happen when you are looking at a 'legit' site.
It's happened to me three times, and not on some suspect site, luckily, I knew what NOT to do after the first time
I think this is how I got a virus while I was at Lolla, and the leaked songs came out: I click a link, a browser opens up that tells me
Warning! a virus has been detected, then lists a dozen virus/trojans that are detected. Then there is "click here to run system scan". Of course, I click it, which downloads the virus. They mimic a virus protection program, and panicked, you instinctively click it.
Point is.
IF you get a virus, your antivirus woudn't be telling you that in a browser window. Close your window (upper right X) then scan your computer using your anti-virus software
The Virus disabled my anti virus, closed any windows I tried to open within seconds (to prevent me from going online to fix it) and basically rendered my computer useless, unless I bought their 'antivirus software' enter credit card etc....
Luckily, I had a restore point to go to, after hours of trying anything to clear the virus, then ran my scan to remove the Trojan
Bottom line, as alarming as a virus warning is, take a breath, then make sure it's your anti virus software telling you and not your browser. (Restore Points are your friend too)
-Also, if a website asks you to download software to run something, it's always more prudent to go to the legit site for that download and get it from there... ie Adobe for flash