These elements and molecules will likely find their way into new stars and planets as they form from molecular clouds. Compare the spectra of the star to the spectra of a comet in our own solar system. Many of the emission features are the same. The continuum curve is lower for the comet because it has a lower temperature than the star does. Many of the molecules formed around old stars make it into stellar systems - our own solar system contains material created in the environments around stars extinguished before our solar system formed.
Stars eject their material into space in a variety of ways.
Most stars will shed their outer atmospheres in layers which we see as planetary nebulae or eject material via high velocity winds. Large stars will explode, creating spectacular supernova remnants. To the right is the infrared spectra of the planetary nebula NGC 6543 taken by the ISO satellite. It clearly shows the fingerprints of sulphur, neon, and argon.