Actually it is not magnetic but paramagnetic. That is that it is attracted by the magnetic field but does not remain magnetic once it leaves the field. Gaseous oxygen is paramagnetic also but is moving too fast to be affected by the magnets. The reason that it is paramagnetic is because the oxygen molecule has two unpaired electrons. Electrons not only go around the atom in their orbitals, they also spin, which creates a magnetic field. Unpaired electrons spin in the same direction as each other, which increases the magnetic field effect. When the electron in an orbital become paired with another electron in that orbital, the new electron spins in the opposite direction and this cancels the effect of the first electron. Note that according to Valence Shell Electron Pair Repulsion theory (VSEPR), O2 has no unpaired electrons but according to Molecular Orbital (MO) theory it does have unpaired electrons. Since liquid O2 does stick to a magnet, MO theory is better at explaining the behavior.
Mark Lockhart, B.S., High School Chemistry Teacher
Firstly, let us define the properties of the oxygen we'll be talking about. O2 has, in total, 12 valence electrons (each oxygen donating six).
For something to be magnetic (we say 'paramagnetic'), it must have an inequality in the total electron spin. The quantum number ms represents the magnetic spin of an electron. It can have values of 1/2 or -1/2, and is an important number when dealing with bonding and the Pauli exclusion principle. When an atom or molecule has an equal number of 1/2 and -1/2 spins such that they cancel each other out, it is not magnetic (we say 'diamagnetic'), and this can be determined from how the different electron shells are 'filled up' by the electrons.
The VSEPR & Valence Bond theories do not explain O2's magnetic nature. However, experiment reveals it most certainly is! Molecular Orbital Theory (MO Theory) is needed to understand how O2 is magnetic. Teaching the basics of MO Theory would take far more time than can be devoted here, so I'll supply a link or two at the bottom for anyone who wants to learn more. :)
Anyway, the valence electrons fill the molecular orbitals in much the same fashion as in other bond theories, and the Exclusion Principle still holds, but these orbitals have different names. The order in which O2 will fill the orbitals is:
sigma2s, sigma2s*, sigma2p, sigma2p*
Two electrons can occupy each s orbital, while 6 electrons can occupy each p orbital. Following the Exclusion Principle, two electrons will fill both the 2s and 2s* orbitals, 6 electrons will fill the 2p orbital, and that leaves 2 electrons to fill the 2p* orbital. These two electrons will only partially fill this orbital, and will have parallel spins. Since the rest of the electrons are all paired, the remaining two electrons in the 2p* orbital give the diatomic molecule a net total spin (it does not matter if they are 1/2 or -1/2 spins, they will both be the same). Since there is a net spin, O2 is paramagnetic.
Since this isn't really the place to learn MO Theory, if you wish to learn more, see the following site:
'One cannot help but be in awe when he contemplates the mysteries of eternity, of life, of the marvelous structure of reality. It is enough if one tries merely to comprehend a little of this mystery every day.'