The heat from the flame excites the valence electrons into a higher-energy shell. The electrons then spontaneously return to their previous, more stable shell and release the energy in the form of visible light.
The specific color changes for different elements are because of the differences in the individual electron shell energies. Electrons can only change shells with the adsorption or emission of specific amounts, or quanta, of energy.
Thus, whatever the energy (and color) of the flame, once an electron has been excited to a higher-energy shell it can only return to a lower-energy shell with the release of a very specific energy and wavelength of emitted light. This emitted light is what is observed as a change in the flame color.
A handy infographic of elements and flame colors can be found here:
Other examples and a good discussion of what is happening, and how to perform the tests safely is here:
WHICH one your particular sample is depends on the provided materials. Generally, for a school lab expensive or very reactive materials will be avoided.