During the Big Bang, only the two simplest elements formed - Hydrogen and Helium. The remaining 92 naturally occurring elements created in stars.
Since different elements have different numbers of protons and neutrons, to construct different elements, we must alter the nuclei. Under the appropriate conditions, certain combinations of elements can be fused together to make heavier elements.
The mass of the parts in this case is actually larger than the mass of the product. That excess mass is converted into energy according to Einstein's famous formula
(E = energy, m = mass, and c = the speed of light). The more protons an element contains, the more difficult fusion becomes since the initial repulsion of the protons must be overcome for nuclei to merge.
Within stars, the high temperatures allow the fusion of elements, beginning with hydrogen at continuing through that of iron. The remaining elements require much energy to form, and that energy is found in supernovae explosions. Elements heavier than iron form in the few seconds of a large stellar explosion and are subsequently less abundant.
Supernovas occur when the fuel in massive stars runs out and catastrophic gravitational collapse occurs. Within minutes, the star reaches temperatures of tens of billions of degrees and explodes.