Why do different elements produce different colors of flames?

A good question.The build of an element causes it to reflect a wavelength of light.

The colored flames are caused by the outer electrons jumping to a higher energy level when the ion is put into a flame.They can be identified by the wavelength or color of light they fall back to.

There are compounds that produce the colorful flames.Due to the heat of the flame, the electrons in those compounds are excited to higher energy levels.The amount of energy they absorb is expressed as follows.

They lose a specific amount of energy, equal to hf, when they go down an energy level.The flame appears to be different.A colorful light is just being emitted from the flame, where you dropped the chemical compound.The fire will appear green if you put barium chloride in it.

When the electrons lose energy, they emit it in a form of radiation with a wavelength that allows it to be visible light.