The reason for blue flame and red flame is a bit complicated higher level study.
I will try to explain it in simplest possible way.
Every atom or molecule has a particular colour associated with it. When atom is heated, the electrons will get more energy from heat, a process called excitation. This energy absorbed as heat is relesed as light, and each atom/molecule releases particular light.
So, oxygen releases bluish green light when heated(excited).
When complete combustion occurs, there should be a lot of Oxygen, and a lot of CO2 will be formed, with a lot of heat released. This heat causes Oxygen and CO2 to excite, and their combined colour is blue.
When incomplete combustion occurs, oxygen will be less, and some,CO is formed. This CO will release some yellowish light energy, and so, we can see green flame. ( blue+orange gives greenish light)
When very bad combustion occurs, we have very low Oxygen. So, no blue light is formed. Only yellow and orange light of CO and C is seen.