Burning is a chemical reaction that produces energy and gases. Some of this energy is emitted as heat, and the rest of the energy is absorbed by the gas around the flame. This energy leads to electrons going into higher energy states. The electrons then drop back into lower energy states and when they do this they emit photons that we see as light.
When an object is heated or burnt (depending on what type of material is it made out of) it will usually emit electromagnetic radiation: usually in the infrared spectrum (heat), but also as visible light (the object may glow). As Sarah said, this is due to energy transitions in the material’s electrons — when they absorb the heat energy, they jump to higher energy levels, but drop back down and re-emit the energy in specific frequencies.
The electron energy states that Sarah and Tom mention are usually responsible for the specific colours of flames. However, any hot object will also emit blackbody radiation. Any black body will emits a range of wavelengths. For cold objects very little light is given off, but at the object heats up, it can start giving of light. This typically starts at about 600 to 700 celsius with a dull red, going up to white at about 6000 celsius and even blue at 10000 celsius.
Although blue flames (like the burner on a gas cooker) are mostly spectral, as Tom and Sarah explained, a sooty flame (like some old oil) also has this blackbody radiation too. What is happening here, is that the smoke particles are heated in the flame so that they start to glow.