The filling of atomic orbitals by electrons are governed by the three major guidelines- Aufbau principle; pauli's exclusion principle and Hund's rule.
Aufbau principle : Electrons are filled in various orbitals in the increasing order of their energies.
pauli's exclusion principle : No two electrons in an atom can have all the four quantum numbers identical. Each orbital can accomodate at the most two electrons with opposite spin.
Hund's rule ; This operates when filling orbitals having identical energies. Electrons enter one by one till all of them are singly occupied with parallel spins. This is followed by the double occupation of these orbitals.