There are two basic reasons for the colours of flames. The first, and possibly simpler (though very little to do with flames really is simple) is that the hotter things get, the greater the proportion of the light that they emit is of a high frequency, that is to say, towards the blue end of the spectrum.
However, you easily can tell that that is not the whole story. Heat a tungsten filament to say, 500°C and it becomes dark red. At about 800°C it is cherry red, at 1000°C it is orange, and about 1300°C it is white hot.
One thing to note is that we do not see just different light frequencies, but added light frequencies. In contrast, if I burn methane in a low concentration of oxygen, I get a smoky yellow flame, whereas if I give it an excess of oxygen, the flame goes almost invisibly blue. This is a pattern of colour change radically different from what we saw in heating the metal. Without going into the differences between the ways in which hot gases and hot solids generate light, just accept that a flame is more than just a hot gas; it is a mix of molecules, ions, and often particles, that are reacting in different ways and at different temperatures in different parts of the flame. So, for example if I burn paraffin wax to light a room (typically in a candle), then the light comes from the yellow part of the flame. The yellow part of the flame is yellow because that is where the paraffin vapour is breaking down and releasing soot, particles of carbon that burn more slowly than the hydrogen from the paraffin, and glow yellow as they burn.
If I add enough oxygen then the reaction runs faster and hotter, and the paraffin burns completely before it has a chance to form soot particles that can glow yellow. The flame burns smaller, hotter, and an almost pure blue. It is a more intense blue than we had before, but it does not look as bright as the yellow of the glowing carbon particles, because our eyes are more sensitive to the yellow of carbon at that temperature.
This is one typical example of how the changes in the components of a flame burning at a different rate and different temperature cause it to emit very different wavelengths of light.