Flame tests are used to identify the presence of a relatively small number of metal ions in a compound. Not all metal ions give flame colours.
For Group 1 compounds, flame tests are usually by far the easiest way of identifying which metal you have got.
Flame colours are produced from the movement of the electrons in the metal ions present in the compounds.
For example, a sodium ion in an unexcited state has the structure 1s22s22p6.
When you heat it, the electrons gain energy and can jump into any of the empty orbitals at higher levels - for example, into the 7s or 6p or 4d or whatever, depending on how much energy a particular electron happens to absorb from the flame.
Because the electrons are now at a higher and more energetically unstable level, they tend to fall back down to where they were before - but not necessarily all in one go.
An electron which had been excited from the 2p level to an orbital in the 7 level, for example, might jump back to the 2p level in one go. That would release a certain amount of energy which would be seen as light of a particular colour.
However, it might jump back in two (or more) stages. For example, first to the 5 level and then back to the 2 level.