The major tool in investigating the composition of an astronomical body is spectroscopy. This technique makes use of the fact that a body composing certain elements/compounds will shine more brightly(or less) at particular wavelengths. The pattern of 'lines' taken by a spectrograph can then be used to infer the original composition. This technique is used on Earth as well for calibration and has a pedigree of over 100 years.
A famous example is the discovery of helium, it's existence was discovered in the Sun before on Earth! This was done by analysing the solar spectrum, and noticing particular dark bands. These dark bands were where light was being absorbed by the Helium in the solar atmosphere.
The composition of extra solar planet should be taken with a pinch of salt, the science is very new and uses instruments not designed for the task. Keep an eye out for new missions such as ECHO which might fly in the next 5 to 10 years...
Apart from stellar spectra we can also observe something called the 21cm line. This a particular emission of Hydrogen, and it helps map out enormous clouds of hydrogen. The science of astronomical compositions is quite battle hardened, it has survived many years of experiment and undergone much refinement.