Elements form in stars through a process called nucleosynthesis. During a star's life cycle, nuclear fusion reactions in its core convert hydrogen into helium. In more massive stars, further fusion reactions create heavier elements like carbon, oxygen, and iron. When stars explode as supernovae, they scatter these elements into space, enriching the interstellar medium and contributing to the formation of new stars and planets.