The valence electrons are the electrons in the outermost electron shell of an atom.
That is why elements whose atoms have the same number of valence electrons are grouped together in the Periodic Table.
Generally, elements in Groups 1, 2, and 13 to 17 tend to react to form a closed shell, corresponding to the electron configuration #s^2p^6#.
This tendency is called the octet rule, because the bonded atoms have eight valence electrons.
The most reactive kind of metallic element is a metal from Group 1 (e.g., sodium or potassium).
An atom in Group 1 has only a single valence electron. This one valence electron is easily lost to form a positive ion with an #s^2p^6# configuration (e.g., #"Na"^+# or #"K"^+#).
A metal from Group 2 (e.g., magnesium) is somewhat less reactive, because each atom must lose two valence electrons to form a positive ion (e.g., #"Mg"^(2+)# with an #s^2p^6# configuration.
Within each group of metals, reactivity increases as you go down the group.
The valence electrons are less tightly bound and easier to remove, because they are farther away from the nucleus of the atom.
A nonmetal tends to attract additional valence electrons to attain a full valence shell.
It can either share electrons with a neighboring atom to form a covalent bond or it can remove electrons from another atom to form an ionic bond.
The most reactive kind of nonmetal is a halogen such as fluorine or chlorine.
It has an #s^2p^5# electron configuration, so it requires only one additional valence electron to form a closed shell.
To form an ionic bond, a halogen atom can remove an electron from another atom in order to form an anion (e.g., #"F"^"-", "Cl"^"-"#, etc.).
To form a covalent bond, one electron from the halogen and one electron from another atom form a shared pair (e.g., in #"H–F"#, the dash represents a shared pair of valence electrons, one from #"H"# and one from #"F"#).
Within each Group of nonmetals, reactivity decreases from top to bottom, because the valence electrons are at progressively higher energies and the atoms do not gain much stability by gaining electrons.
In fact, oxygen (the lightest element in Group 16) is more reactive than chlorine, even though it is not a halogen, because the valence electrons of oxygen are closer to the nucleus (at a lower energy).
Therefore, a metal from the bottom of Group 1 (like potassium) and a nonmetal from the top of Group 17 (like fluorine) will react violently, because they both benefit greatly from the reaction.
#"K"# loses one electron to #"F"# and forms the ionic compound potassium fluoride, #"K"^"+""F"^"-"#.