An electron configuration lists which sublevels an atom's electrons occupy. Three rules decide the arrangement.
The aufbau principle says electrons fill the lowest-energy sublevels first. The usual filling order is 1s, 2s, 2p, 3s, 3p, 4s, 3d, 4p, 5s, 4d, 5p, 6s, 4f, 5d, 6p, 7s, 5f, 6d, 7p. For neutral potassium and calcium, 4s is occupied before 3d. Orbital energies shift as electrons are added, so transition-metal ions generally lose 4s electrons before 3d electrons. Sublevels hold s = 2, p = 6, d = 10 and f = 14 electrons.
The Pauli exclusion principle says one orbital holds at most two electrons, and they must have opposite spins. Hund's rule says electrons spread out one per orbital, with the same spin, before any of them pair up. So nitrogen's three 2p electrons sit in three separate orbitals.
The periodic table maps this filling order. Groups 1-2 and helium form the s-block, groups 13-18 except helium form the p-block, the transition metals form the d-block, and the two detached bottom rows form the f-block.
Worked example. Iron has 26 electrons. Fill in order to get 1s22s22p63s23p64s23d6 (2 + 2 + 6 + 2 + 6 + 2 + 6 = 26). In noble gas shorthand, argon covers the first 18, giving [Ar] 4s23d6. For Fe²⁺, remove the two 4s electrons first, giving [Ar] 3d6.
Chromium and copper are common exceptions, written as [Ar] 4s13d5 and [Ar] 4s13d10.