Eventually, the hydrogen and helium inside a massive star’s core will be short of supply.
When this happens, the star contracts and its core gets hotter - hot enough to fuse helium, producing carbon. Next, even heavier elements than carbon build up at the centre, until the fusion of heavy elements (such as iron) does not produce any additional energy.
Finally the star implodes, its outer layers bounce off the core and are expelled into space.
Over billions of years, unimaginable numbers of stars have lived and died, leaving behind vast nebulas that contain heavier elements like carbon, oxygen and nitrogen. These nebulas are the breeding grounds for new stars and planets. Our galaxy contains many nebulas. You can even see them with a small telescope on a clear night.