We use a two-color transient Kerr rotation technique to study the spin dynamics in an n-doped CdTe∕Cd0.85Mg0.15Te quantum well. The dynamics displays the interplay between excitons, trions, and the two-dimensional electron gas. The spin relaxation of individual species is resolved by spectral selection. The spin dynamics are quantitatively described by rate equations involving the spin populations of excitons, trions, and the electron gas. Under resonant excitation of excitons, spin polarization of the electron gas is generated through trion formation, with the spin coherence partially lost through exciton spin relaxation. A maximum hole spin-flip time is observed around the trion resonance, with a rapid decrease for increasing excitation energy.