The s and p sub-shells contain 8 electrons in total, and a full complement in the outermost of these is the most energetically stable. Therefore, elements in a compound attain the electron configuration of the noble gas nearest to them in the periodic table, and bonds between atoms form according to this rule.