Octet rule states that <em>"it is the tendency of the atom to have eight electrons in its outermost valence electron shell</em>".
Nonmetals obey octet rule by gaining electrons to its outermost valence electron shell.
For example:Cl which has 17 electron and its electronic configuration is 2,8,7 and it gain electron to have 8 electrons in its outermost electron shell to form Cl⁻ with electronic configuration of 2,8,8.