The number of electrons in an atom's outermost valence shell governs its bonding behavior.
A nonmetal atom tends to attract additional valence electrons to attain a full valence shell; this can be achieved in one of two ways: An atom can either share electrons with a neighboring atom (a covalent bond), or it can remove electrons from another atom (an ionic bond).