A magnesium atom tries to lose 2 electrons and a chlorine atom tries to gain one electron. So the magnesium atoms give one electron to a chlorine atom making that atom have a full outer shell and then give that other electron to another chlorine atom making that one also have a full. Since the magnesium atom has now lost 2 electron, it also has a full outer shell making hem all ions.