The flame test is used to visually determine the identity of an unknown metal or metalloid ion based on the characteristic color the salt turns the flame of a Bunsen burner. The heat of the flame excites the electrons of the metals ions, causing them to emit visible light. Every element has a signature emission spectrum that can be used to differentiate between one element and another.
Metals are composed of individual atoms, which contain a nucleus of protons and neutrons and outer shells of electrons, which float freely around the nucleus. Metal atoms usually exist as ions, or an atom with a charge. Metal ions are usually cations, meaning they carry a positive charge.
The electrons of metal ions circle the nucleus in layers called orbitals. As the orbitals get farther away from the nucleus, their energy level increases. Electrons need to absorb energy to move to a higher energy orbital.
As it turns out, this is exactly what happens during the flame test. During the flame test, metal ions are exposed to thermal energy, or heat, from the flame. The electrons in the outer orbital absorb the energy and temporarily bounce to a higher energy level.