Magnesium is an essential mineral involved in hundreds of enzyme reactions — muscle contraction and relaxation, nerve signalling, heart rhythm, bone, and energy/metabolism. It works alongside other electrolytes like calcium and potassium.
It's found in leafy greens, nuts, seeds, legumes and whole grains. Many people fall short of the recommended intake, though overt deficiency is less common in healthy people — see magnesium deficiency. As a supplement it's popular for sleep, mood and cramps (evidence is modest), and forms differ in absorption — see magnesium glycinate and types of magnesium.