Our sun began it's life as a rotating cloud of interstellar dust and gas. Matter began a gravitational collapse.
Because of the rotation, the cloud flattened and much of the matter became concentrated at its center. Gravity fundamentally depends upon mass and distance; the more massive an object, the stronger the attractive force it exerts on other objects.
At the center, strong gravitational forces confined particles to a relatively small space. As a result, temperatures increased.
Over time, enough matter collapsed upon the center that it was heated to the point where nuclear reactions could begin. A star was born.
It is this balance between pressure and gravity that keeps a star like our sun stable.
The original atoms formed in the Universe within the first three minutes of the Big Bang were only hydrogen and helium. Carbon and oxygen -- the atoms making up carbon monoxide -- had to be made in the thermonuclear furnaces at the cores of the earliest stars.