Aufbau principle
The Aufbau principle — from the German word for 'building up' — is the rule that an atom's electrons fill the lowest-energy orbitals first, only moving to higher ones once the lower seats are taken. Imagine pouring water into a series of cups at different heights: it settles into the lowest first. Following this order, starting from hydrogen and adding one electron at a time, lets you build up the electron configuration of any element.
The filling order is not simply by shell. Because subshells of different shells overlap in energy, the sequence weaves between them: the 4s subshell, for example, fills before the 3d, captured in the well-known diagonal rule that orders subshells by the sum of their principal and angular numbers. Combined with the exclusion principle, which caps each orbital at two electrons, this gives a reliable recipe for the ground state.
It is worth treating the Aufbau principle as a dependable rule of thumb rather than an exact law. The true ground state is whatever arrangement has the lowest total energy once every electron's attraction and mutual repulsion is accounted for, and for a handful of elements the simple filling order gets it slightly wrong. The principle works because, most of the time, filling the lowest single-electron levels does come closest to that true minimum.
Subshells fill from lowest energy up, with 4s slipping in ahead of 3d.
Once 3d electrons are present, they actually sit lower than 4s, so ions of transition metals lose their 4s electrons first — a reminder that the filling order is not a fixed energy ranking.