Element X is oxygen (atomic number 8). Oxygen needs two more electrons to complete its noble gas configuration. The element Y is magnesium (atomic number 12). Magnesium has two extra electrons than are needed to achieve a stable configuration. Hence, magnesium donates two electrons to the oxygen atom to attain a stable configuration.
So, Mg becomes Mg2+ and O becomes O2- as ions.