The key to understanding this is forbidden line emission. Certain transitions between quantum states are "forbidden" to occur via the most efficient electric dipole mechanism. The "selection rules" that govern which transitions are forbidden and which are allowed are a consequence of the quantum mechanical nature of the transitions.
If an atom (or ion) is excited into a particular state then it may decay to a lower energy state either radiatively (by emitting a photon) or it may be collisionally de-excited. Which of these two things happens depends on the (electron) density of the gas, the temperature and the radiative lifetime of the excited state.
A forbidden transition has a (comparatively) very long radiative lifetime (maybe as long as a second compared to the $10^{-7}$ s of an allowed transition). This means that in most circumstances the transition will not occur radiatively - hence the term "forbidden line". If the gas is sparse enough, for example in the upper atmosphere, then the collisional de-excitation timescale may become long enough for the transition to occur radiatively.
So what is going on with the different colours is that they arise from different transitions in different species that correspond to particular wavelengths. The particular transitions occur only at particular heights in the atmosphere where the conditions are such that the (i) the species exists, (ii) the upper level of the transition is excited in sufficient numbers and (iii) the density/temperature combination is such that collisions are rare and do not "quench" the forbidden line emission.
The predominance of different colours is not necessarily due to vastly different concentrations of oxygen vs nitrogen at different heights; both green and red aurorae are produced by transitions in atomic oxygen (at 558 nm and 630 nm respectively). It is that the different coloured transitions are quenched at different heights in the atmosphere. The 630nm has a longer radiative lifetime and is quenched at higher densities in the lower atmosphere.
On the other hand atomic oxygen is rarer lower in the atmosphere and transitions in ionised (molecular) nitrogen become more dominant. These can either be blue or red, again, depending on the density of that particular layer of atmosphere as different forbidden transitions are excited and quenched.
In general the conditions to excite and radiate a particular transition are only met in fairly narrow, stratified bands that are different for different species/transitions and this results in the coloured bands of the aurorae.
Further reading can be found here.