Magnesium has the atomic number 12. There are 12 electrons in the outermost shell. Mg atom would look like this Mg: (if we ignore the full shells). This molecule is very reactive, as it wants to reach a full shell, and it can do that by losing those two electrons.
When magnesium is reacted with sulfuric acid H2SO4, it gives these electrons to the two H+ ions, forming H2 gas, converting Mg: to Mg2+. This reaction proceeds because Mg: is more reactive than H2 towards giving electrons (acting as a reducing agent). H2 is content as it has full shells for its shared two electrons, and Mg is happier (more stable) as the +2 charge than as Mg:.
An illustration of the molecule sulfuric acid is as below. The oxygen atoms on the S=O bonds have two electron lone pairs, while the oxygen atoms in the S-O- bonds have three lone pairs. Two hydrogen atoms have donated their electrons to oxygen. Hydrogen has a +1 charge, while oxygen has a -1 charge. The molecule is ionic because here is no sharing of electrons like in the sulfur - oxygen bonds. If you are studying the octet rule, sulfur is one of the exceptions to the rule because of d-orbitals.
Hope this helped.