Electrons on atoms have different amounts of energy proportional to the distance of their orbital from the nucleus. Electrons (which are negative) close to the positive nucleus have lower potential energy; those in "higher" energy levels farther away have more energy. In order for an e- to "jump" from a lower level to a higher one it must absorb energy, often in the form of light. Conversely when an e- "falls" from a higher level to a lower one, it gives off energy, again in the form of a photon of light.
The amount of energy either absorbed logically depends on the distance the electron "jumps" or "falls". But the e- always absorbs or releases exactly one photon of light, not lots of photons for a big change in energy but a few photons for a small change in energy. How can this be? This is where the color comes in: photons with a high frequency have lots of energy, photons with low frequency have little energy, and we perceive photons with high frequency as bluer and those with lower frequencies as redder ( with all the colors of the rainbow in between as in ROY G BIV ).
OK. So in the flame, electrons get excited and pushed to higher energy levels by the heat energy. When they fall back down, they give off photons of light of different colors, based upon how far they fall. Different temperatures cause electrons to jump to different levels, but different types of atoms also have energy levels that are different distances apart. Thus putting copper into a flame causes a green glow because electrons on the copper atoms are falling and jumping exactly the right distance to emit or absorb photons of the frequency we see as green (you can try this with a penny)
The same idea explains not only color in flames, but all the colors we see.
Answered by: Rob Landolfi, None, Science Teacher, Washington, DC