J.R. S. answered 04/24/19
Ph.D. University Professor with 10+ years Tutoring Experience
The simple answer (excluding exceptions) can be found by reading about the Aufbau principle. It essentially states that in the ground state of an atom or ion, electrons fill atomic orbitals of the lowest available energy levels before occupying higher levels.