Chemicals in the Sun's Atmosphere
The spectrum of the Sun, or most other stars, reveals that the atmosphere of the star (above the photosphere) contains a rich mixture of chemical elements. However, the predominant elements (making up more than 95% of the atoms in the sun) are hydrogen and helium. The nuclear reactions taking place on the interior of the star produce heavier elements--but not much heavier. Some of these elements can diffuse through the star, and appear in the atmosphere. However, really heavy elements, like silicon, iron, and nickel, which are fairly abundant in the Sun, are not produced in great quantity by the Sun's own nuclear reactions.
The other elements, and particularly the heavier ones were present in the gas that originally condensed to make the Sun in the first place. Most of these were produced in the explosions of other nearby stars before the Sun was formed.
That brings us to all the other elements seen in the solar spectrum. Some of them are produced in the core of the star, by fusing lighter atomic nuclei. More than that, the Sun formed in a region of space where there have been other stars burning, and some supernova explosions (which produce huge amounts of heavy elements, including those that cannot be produced any other way), which have left behind a gas fairly abundant in these heavier elements. Thus, when the gas gathered to form the sun, it already had many of these metals in it. It turns out that this greatly accelerates star formation, compared to a gas with very small amounts of heavy elements.
The sun is a fairly "dirty" star. Some stars have almost no detectable elements heavier than helium. However, since they are slow to form, they are much less common than dirty stars.