How does carbon dioxide or water vapour absorb thermal infra red radiation from the sun? We are all told at school water vapour and carbon dioxide are the top two greenhouse gases, and that they absorb thermal infra red radiation, trap heat and warm up the Earth. My question is how do they do that? Why can't Oxygen or Nitrogen or any other gas not absorb infra red radiation as well as water vapour or CO2?
 A: The absorption of IR radiation is due to vibrations of molecules. When a vibration causes change in charge distribution (or dipole moment to be more specific) the IR radiation is absorbed. 
Generally, hetero-polar molecules, like $\rm H_2O$ and $\rm CO_2$, have permanent dipole moment. The external oscillating electric field in this case perturbs the Hamiltonian and causes IR absorption. Hence, they contribute to "Green House effect" by absorption of heat.
$\rm H_2O$, which is non-linear molecule, has three fundamental modes of vibrations. Symmetrical Stretching, asymmetrical Stretching and scissoring (bending). $\rm CO_2$, which is linear molecule, has four fundamental modes of vibrations. Symmetrical stretching, asymmetrical stretching and two degenerate scissoring modes, in planes perpendicular. The symmetric stretching mode in $\rm CO_2$ does not produce or absorb any IR, as it does not cause change in dipole moment, but other modes do change charge distribution causing absorption of IR.
Well, the homo-polar molecules, like $\rm N_2$ and $\rm O_2$, does not have any permanent dipole moment. The external oscillating electric field does not perturb the Hamiltonian for nuclear motion, and does not absorb IR, though it perturbs Hamiltonian for electronic motion. Hence, do not contribute for "Green House effect".
A: As you can see on these absorption spectra for $\textrm{H}_2\textrm{O}$ and $\textrm{C}\textrm{O}_2$, both molecules have moderate to strong absorbtion in the mid-IR wavelengths, with the absorption of $\textrm{C}\textrm{O}_2$ extending out into the longer wavelengths.
Other molecules common in the atmosphere don't have such strong absorption at the wavelengths given off by thermal radiation. If you are asking why that is, I'm afraid I can't give you a very detailed answer, except to say that the absorption spectra of molecules (and atoms) is governed by quantum mechanics. Maybe somebody else can explain how they would be calculated from principles, but that is beyond my education.
A: CO2 and H2O absorb a lot of IR radiation because they have internal vibrations with the same energy as infrared photons.  
Every molecule has a different set of internal vibrations, and those match the frequencies that it absorbs and emits most readily.  When the photon is emitted, the molecule loses the energy in that vibration, and the energy is transferred to the photon.  
Essentially it boils down to conservation of energy.  
A: Since CO2 re-radiates IR energy, it can't be said that it "absorbs" the energy. It just converts it to another frequency in the electromagnetic spectrum. The end result is transparency.
