When heated or exposed to electricity, the electrons in atoms of a given element gain energy and move to a higher energy level or orbit; they do not maintain this position but re-emit the energy as light of a specific wavelength, generating color. For elements in a solid or liquid state, this light generally appears as a rainbow while elements in a gaseous state reveal their atomic emission spectra, the specific bands of color produced by that elements particular electron configuration.
When an atom is heated it gains energy. This energy is absorbed by the electrons and it causes them to move to higher energy levels or orbitals. The electron then drops back down to its original energy level, releasing a photon with a wavelength that corresponds to the energy gained and then lost by the electron.
The emission spectrum of a particular element is the characteristic wavelengths of light produced by the atoms of that element when heated as a gas. These wavelengths are distinctive because the different electron configurations in different atoms cause different wavelengths of light to be emitted. Which wavelength is predominant in the emission spectrum of an element will determine what color you see when that element is heated as a gas.