If it is plugged into an infected computer, wouldn't the secret key be compromised?
The yubikey stores the key on its internal storage media -- the exact mechanism varies depending on the type of key. When yubikey is plugged into a computer, it is recognized as a USB keyboard. Pressing the sole button on the yubikey will send a string of characters containing your one-time password, just as if they were typed via a USB keyboard. The manufacturer assures that the secret key used to generate one-time passwords can never be read off of the device, regardless of the software installed on the computer it's plugged into.
Also, is it possible for a computer virus to "infect" a YubiKey?
Everything is possible, but would be very hard considering the limited functionality of a yubikey.