Magnesium is a metal that brings two valence electrons in its outer shell. It wants to lose these two electrons so that its “outer shell” is empty.
Sulfur is a non-metal that brings six valence electrons with it; it wants to gain exactly two electrons to complete its outer shell.
This makes Magensium and Sulfur a great match for each other; magnesium gives its two valence electrons to sulfur, and that’s it.
You must show square brackets around each ion, along with its charge, to emphasize that you are not creating a molecule. You are creating an ionic lattice of alternating positive and negative charges.