When we observe a star directly without any obstruction between us and the star, we see light of all colors, so the spectrum of the star looks continuous, without any gap. If we observe a diffuse cloud of gas directly, we only see light of specific colors, so the spectrum of the gas looks discrete, full of gaps. And if we see the light coming from a star through a cloud of gas, some colors are absorbed by the cloud leading to a continuous spectrum with some gaps. The gaps show the missing colors that have been absorbed by the atoms and molecules of the gas. In physics and chemistry, we call them absorption lines because they appear as dark lines in a spectrum. In the brightness-wavelength plot, we see a sudden drop in brightness at specific wavelengths corresponding to the missing colors.
The absorption lines arise because the electrons inside the atoms and molecules jump from lower a energy-level to a high-energy level or sometimes completely escape from an atom by absorbing the light of a specific color, frequency or wavelength. The opposite also happens. An electron inside an atom can jump from a higher-energy level to a lower-energy level by emitting or radiating light of a specific color. The spectrum of a cloud of gas is an emission spectrum, full of emission lines. The light from a cloud comes only because of some specific electrons that are going to some specific lower levels emitting light of some specific colors. Note that the absorption spectrum is just an inverted emission spectrum.
The chemical elements found in the sun and their abundance relative to silicon (Si). The abundance of silicon is taken to be $10^6$ and all other abundances are measured with respect to this. The numbers on the vertical axis are actually powers of 10. For example, the abundance of H is more than $10^{10}$, or more than 10,000 times higher than Si, and so on. You can see clear peaks around H, O, Fe, Ba and Pb. Some corresponding absorption lines are given above the abundance plot. These black lines were first found by the German physicist Fraunhofer in the 19th century.
Where did all these elements in the sun come from? Were they produced by the sun? No. Our star is not that heavy, it cannot produce anything much heavier than He. So all these elements were already present in the interstellar cloud that gave rise to our solar system. Where did the cloud get them? From nearby supernova explosions. Nearby heavy stars created these elements and enriched the neighboring clouds after their death.