Example: NaCl. Every element in group 1, including sodium, has one electron in its outer shell and it wants to lose this one electron to have only full shells. Everything in group 7, including chlorine, has 7 electrons in its outer shell and wants to accept one electron to have a full outer shell of 8, so sodium will donate its one electron to the chlorine, and both are now happy- you only need one sodium and one chlorine atom to make both atoms happy with full shells, so your formula is NaCl.