The emission spectra of gases can be used to study stars because the absorption spectra of stars are the exact opposite of the emission spectra of the gases that compose the stars. When the light from a star is split into its component colors, it creates a continuous spectrum except for specific missing colors. These missing colors are the same ones emitted by heated gases in laboratory settings.
Light from stars appears white and, when split, contains a large spectrum of colors. Individual gases, however, only emit certain wavelengths of light when heated, according to the way their electrons are configured. When atoms absorb heat, some of their electrons move to larger, more energetic orbitals. When the electrons move back into their resting orbital, they release light energy at particular frequencies.
Stars emit light and a much wider range of frequencies than just the heat emission spectra of their component gases. Any color that a light emits, however, it also absorbs. Thus, the light generated by other processes that matches the gases' emission spectra is actually absorbed by the gases around the star. This creates darker regions in the spectra of the star itself and indicates the gases that compose it.