1) All the materials are made up of atoms and molecules. These atoms and molecules possess certain fixed energy.
2) An atom or molecule having lowest possible energy is said to be in ground state.
3) When we heat the materials the electrons of these atoms gain energy and move into excited states (higher energy state).
4) An atom of molecule in excited state can emit light to lower its energy in order to get stability and come back to ground state.
5) Light emitted in such process have certain fixed wavelength for one kind of atoms.
6) The light emitted by different kinds of atoms are different because the excited states electrons will go are different. So different elements produce different flame colours.