Elements like hydrogen and helium first formed in the Big Bang. These grouped together to form stars where some of the heavier elements are formed.
Stars convert one element to another in a strict order.
At first they convert hydrogen atoms to helium - which gives off a lot of energy. Then, once they've run out of hydrogen, they start converting the helium to carbon. This doesn't release quite as much energy. This process carries on through the periodic table until you reach iron. Here the process stops because to convert iron to the next in the series takes more energy than you get out at the other end.