During the big bang, conditions were just right to create Hydrogen and Helium. Gravity collected these elements to form stars.
Lightweight stars like our sun got hot enough to form Carbon and Oxygen... that is all they could do.
More rare, heavier weight stars got hot enough to form the next series of elements. Once these stars reached a certain amount of iron (Fe), they could no longer produce energy. They became unstable and began to collapse in on themselves. Consequently, they exploded into super novae...
All of the rest of the elements were formed.