Noble gases, which have a completely filled valence shell, show little chemical activity. Hence, elements have a tendency to attain a completely filled valence shell by either losing or gaining electrons.
Metals which usually have 1-3 electrons in their valence shell, tend to lose them to attain its nearest noble gas octet configuration.
Nonmetals which usually have 5-7 valence electrons tend to gan 1-3 electrons to attain stability.