To add some more detail. Almost all the elements heavier than Helium (known as "metals") are produced inside stars. To get back into the ISM you have to get the "processed" material out of the centres of the stars. This occurs in basically three at least 4 ways. (i) Supernovae - the explosions associated with the final collapse of very massive stars ($>10 M_{\odot}$). The supernova progenitors have short lives $<50$ million years, so many generations of such stars can have lived and died before the Sun was formed, and probably of order a few hundred million to a billion had done so. (ii) Giant star winds. These occur towards the ends of the lives of stars with mass of $<10M_{\odot}$. Some fraction of processed material gets dredged out of the centre and expelled into the ISM. The lifetimes of these stars is 50 million to $>10$ billion years. Most of the enrichment is caused by stars about in the middle of this range. These stars go on to fade as white dwarfs. (iii) Type Ia supernovae. Explosions triggered by mass transfer on to a white dwarf (probably in binary systems) causing a detonation and the scattering of metals into the ISM. Progenitors have lifetimes similar to the giant stars in (ii). (iv) Neutron star mergers followed by kilonava explosions are thought to produce much of the very heavy elements (beyond zinc and copper). The progenitors of neutron stars are the same short-lived massive stars of mechanism (i) but there is an additional delay of hundreds of millions or even billions of years whilst the neutron stars lose energy through gravitational waves and spiral into each other.
The numbers of white dwarfs we see in the Galaxy and the deduced rate of supernova explosions that are occurring (e.g. from counting the number of neutron star remnants) also matches up with what is required to produce the metallicity of the ISM and the Sun. But there are plenty of variables and unknowns and not everything is solved. For instance it looks quite likely that star formation needed to be higher in the past; and some parts of the Galaxy (e.g. the bulge) have a higher metallicity, thought to be due to a period of very vigorous high mass star formation 10 billion years ago.
Further reading: How can there be 1,000 stellar ancestors before our Sun? and https://physics.stackexchange.com/questions/7131/what-is-the-origin-of-elements-heavier-than-iron