The octet rule states that the atoms are most stable when their valence shells are filled with eight electrons. Example- Neon, argon contain 8 valence electrons. However helium also contains fully filled outer shell with 2 electrons, hence it follows duplet rule.
Elements tend to achieve its nearest noble gas electronic configuration to gain stability. This can be achieved either by losing electrons or gaining electrons.
For example- Sodium (2, 8, 1) loses 1 electron to achieve the noble gas configuration of neon (2, 8). Moreover, fluorine (2, 7) gains 1 electron to achieve the noble gas configuration of neon (2, 8).