How does an element in group 2 attain a stable electron configuration?

It loses 2 electrons and becomes a +2 ion.