Octet rule states that "it is the tendency of the atom to have eight electrons in its outermost valence electron shell".
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.