How do astronomers detect gases that are in the atmosphere of exoplanets?
It is only possible to detect gases in transiting Exoplanets. The spectrum of the star is taken when the Exoplanet is not in transit and again when in transit. The differences between the spectrums is due to a small amount of light from the parent star passing through the upper atmosphere of the Exoplanet and being absorbed by gases in the atmosphere.
The effect is VERY small and difficult to detect.