Isaac Newton discovered that passing light through a prism could break it up into its component colors, like a rainbow. A rainbow is simply a low resolution spectrum of the Sun produced by water droplets in the atmosphere.
In 1814 Josef von Fraunhofer (1787 - 1826) built the first spectroscope and used it to observe the spectrum of the Sun. He discovered that the solar spectrum has dark lines, called absorption lines, which are specific wavelengths where light is missing.
Spectroscopy began in earnest in 1859 when Gustav Robert Kirchoff (1824 - 1887) and Robert Wilhelm Bunsen (1811-1899) built an improved spectroscope. Bunsen had recently invented the Bunsen burner and the two scientists used their spectroscope to study the spectra of materials burning in the Bunsen burner. (One wonders if they were pyromaniacs as well as scientists.)
Bunsen and Kirchoff found that there are three types of spectra: continuous spectra, emission (or bright) line spectra, and absorption (or dark) line spectra.
A continuous spectrum has no sharp changes in brightness at different wavelengths, so no specific wavelengths are significantly brighter or darker than the adjacent wavelengths. A hot solid, liquid, or compressed opaque gas will produce a continuous spectrum. For example, an incandescent light bulb glows when an electric current heats a tungsten wire, so as a hot solid it produces a continuous spectrum.
An emission line spectrum is dark at most wavelengths, but certain specific wavelengths are bright. These bright wavelengths are called emission lines or bright lines. Emission line spectra are produced by a hot transparent gas and the wavelengths of the emission lines depend on the chemical composition of the gas. A neon sign glows when an electric current heats neon gas in a glass tube. Looking at a neon sign through a spectroscope reveals the emission line spectrum of neon. Neon signs glow bright red because the spectrum of neon has a large number of lines at red wavelengths. A neon sign that is not red is not really a neon sign. It works on the same principle but contains a gas other than neon.
In astronomy, emission nebulae are interstellar clouds of gas at a temperature of about 10,000 Kelvins. Their spectra are emission line spectra because they are a hot transparent gas.
An absorption line spectrum looks like a continuous spectrum with certain specific wavelengths missing. These dark lines are the absorption lines. An absorption spectrum occurs when light having a continuous spectrum passes through a transparent gas that is cooler than whatever produced the continuous spectrum. The wavelengths of the absorption lines depend on the chemical composition of the cool transparent gas. Most stars have absorption line spectra because their cores are much hotter and more compressed than their surface layers. The hot opaque core produces a continuous spectrum. When this light passes through the outer layers of the star, that are cooler than the core, absorption lines are produced.
Note that each type of atom or molecule has its own unique set of spectral lines that can be either emission or absorption lines depending on the physical condition producing the spectrum. This unique set of spectral lines produces a unique spectral signature for each element or compound and allows astronomers to determine the chemical compositions of astronomical objects. Chemists also use this technique to determine the chemical compositions of unknown samples on Earth, but chemists also have other techniques that are not available to astronomers.
Kirchoff and Bunsen did not know what causes the spectral lines. That required understanding atomic structure.