Well, magnesium probably does play a part in keeping Earth in its orbit, but the chemical compositions of both the Earth and the Sun do not contain enough quantities of magnesium for it to be of significance. 
No, what I am getting at here is the force that keeps Earth in its orbit, GRAVITY.
- If you are five years old and, more likely than not, reading this with mummy and/or daddy, all you need to know about gravity for now is that it makes things stick to the ground. Gravity is something that, unfortunately, makes a dropped ice cream fall to the floor; but it also keeps you from floating away uncontrollably if you jump up.
- If you are ten, you may know that gravity is a force, that does not only exist on Earth, but also on other planets and stars. The gravitational pull of the Moon is what gives us tides on Earth.
- If you are fifteen, and doing GCSEs if you live in England, it is time to get to grips with some equations and numbers*; but first…
The gravitational force attracts things with a mass to each other; whilst standing, the Earth pulls you towards its centre (down), although the surface (ground) prevents you from sinking in. At the same time, because you have a mass too, you are pulling the Earth towards your centre of mass as well. However, due to the fact that Earth is so much larger, you can barely notice the Earth move.
Gravitational force increases when: 
- the masses of the objects involved increases
- the distance between the objects decreases
This centre of mass I am talking about is, for any spherical (round) object, is essentially its centre. On an object that is not spherical, this becomes a little bit tricker, so we shall not look at it right this moment.
- If you are twenty, you will either already know all you need about the gravitational force for your field, or you will have associated yourself enough with the quantum world to completely lose interest in gravity.
As gravity increases with the object’s mass, it is the reason why you and I stick to our planet Earth, and (partially*) why the Earth stays nicely in its orbit around the massive Sun.
*The equation and numbers, as well as the explanation of “partially” be our next focus.
 Chemical composition of Earth, Venus, and Mercury; John W. Morgan and Edward Anders; Proc Natl Acad Sci U S A. 1980 December; 77(12): 6973–6977
 BBC Bitesize, KS3 Science, Forces and Motion (accessed on publishing date)