
"Given that all an advert needs to be is a static image, or maybe an animated one, how or why is there any code at all, of any kind, associated with it?"
Depending on how the ads are embedded, what comes back from the ad-server can be nearly anything. Not just a hyperlinked image file.
As someone mentioned, it could send JavaScript or other client-side script back to the browser to do an auto-redirect or pop up an alert window or something, or it could return an iframe with the malware page embedded.
It could also be a Shockwave Flash object instead of a static/animated image. I'm not all that au-fait with Flash capabilities (bloody hate the stuff), but I wouldn't be surprised if you can make that auto-load another page too.
In several of these cases there's no need to even click anything, just loading the damn thing is enough to make the badness take place.