Electrons are excited when heated and are bumped up to higher "levels." Or n-values, if I remember right. Anyway when the electrons fall back down, there is a wavelength associated with that fall. These wavelengths happen to coincide with those in the visible spectrum which explains why colors are seen. You can read up on this and even look up specific wavelengths of metals on google. You'll want to look up topics in quantum chemistry and also this equation which is very related which I will just throw out for now.
E = hc/lamda
Good luck.
E = hc/lamda
Good luck.
-
If you are wondering why sodium compounds give an orange flame, copper gives a green flame, etc, it has to do with the excitation of electrons in the metal, which gives a specific color when heated.