An example is O2 which is of two oxygen atoms. Now the oxygen atom has a configuration of 2,6. This means that to gain stability, both of these atoms need two more electrons in order to attain stability. Let’s call them oxygen 1 and oxygen 2. So, if oxygen 1 gives two electrons to the other, the other one will become stable with 8 valence electrons, however, oxygen 1 would then have 4 valance electrons which would still leave it unstable.
This is where the covalent bonding happens, so here, the atoms share electrons with each other to make the other atom stable while still not fully giving away the electrons (the shared electrons will orbit both atoms). So, in the case of O2 both oxygen atoms will share two electrons each, so they will still keep their 6 electrons but will experience two more orbiting around them, this gives both atoms 8 orbiting valence electrons which help them both to attain the octet configuration. .