The most probable answer is that it has to do with the number of valence electrons available for bonding. Sodium in group 1 has 1 electron to donate, which chlorine in group 17 (halogens) has a dire need to fill in it's one remaining electron vacancy. Magnesium in group 2 has 2 electrons it will willingly donate, to the carbonate's 2 electron vacancy position in the outer electron shell. Another possible reason is the elements to the far left of the periodic table (such as sodium and potassium) are more metallic in their properties and find it easier to donate "extra" electrons that are left over in their outer electron shells. The elements such as the ones to the far right of the periodic table ( with the exception of group 18, noble gases) are nonmetallic and find it much easier to fill in the few remaining spaces they have, rather than get rid of what they already have.