Electrons around a nucleus cannot have just any energy; they sit in levels labeled n=1,2,3 and so on, moving outward from the nucleus. Shell 1 is closest and lowest in energy, and electrons fill the lowest level with room first, the way water fills the bottom of a glass before the top. The capacity grows with n: 2n2 gives 2, 8, 18 and 32 for n=1 to 4.
Fill sodium's 11 electrons: 2 go into shell 1, 8 into shell 2 and the last 1 into shell 3, written 2, 8, 1. Chlorine's 17 give 2, 8, 7. An element's period (row) on the table equals its number of occupied shells, so both are in period 3. The outermost shell is the part of the atom that meets other atoms, and its electrons are the valence electrons.
A shell's capacity is not the same as the point where it stops filling. Shell 3 can hold 18 electrons, yet argon (2, 8, 8) ends a row and potassium (2, 8, 8, 1) puts its 19th electron in shell 4. The reason is subshells: shell 3 contains 3s, 3p and 3d, and 3d is higher in energy than 4s, so 4s fills first. Shells are also not orbits: an electron in shell 2 has no fixed path, only a region where it is likely to be found.
Think of it like this. Imagine a hotel with its lobby at the nucleus and the shells as its floors. Guests (electrons) take the lowest free floor, and each floor has more rooms than the one below: 2, 8, 18, 32. Above floor 3 the simple order breaks, because some rooms on floor 4 cost less energy than the top rooms on floor 3.
Core idea. Electrons occupy energy levels around the nucleus, filling the lowest first, and shell n can hold up to 2n2 of them. The outermost occupied shell decides how the atom behaves.