Rule 1. aufbau rule - energy levels filled so lowest energy level filled first.
Rule 2. pauli exclusion principle - maximum of 2 electrons may be placed in each energy level. Provided the spins of electrons are of opposite sign.
Rule 3. hunds rule - when energy levels are equal one electron is added to each energy level in turn with their spins unpaired until each energy level has one electron.