Every first-year astronomy student at some point runs up against the sequence of letters “OBAFGKM.” (Remembered with the help of the catchy mnemonic “Oh Be A Fine Girl, Kiss Me.”) All stars are classified according to their spectra and classified with one of these letters, along with some additional information for further granularity. It brings up the question, though: where did this opaque string of letters come from in the first place?
To understand the origin of our modern classification system, we must go back to the very first attempts to construct such a scheme based on the then-novel discipline of spectroscopy. Spectroscopy, as a reminder, is the measurement of an object's spectrum, the chemical fingerprint of an object woven into the colors of the rainbow; it encodes all kinds of valuable information about the object's chemical and physical properties.
Back in the 1860s and ‘70s, an Italian priest and astronomer by the name of Angelo Secchi was one of the first to apply spectroscopy to the stars. He divided stars into five categories based on their spectra. These classes were mostly arbitrary and have since been superseded, but they were an important first step in the process.
In the 1880s, the American astronomy Edward C. Pickering was compiling a catalog of stellar spectra, which resulted in the Draper Catalogue of Stellar Spectra. His assistant the Scottish astronomer Williamina Fleming divided Secchi's five classes into more specific classes with letter headings running from A to N, with a few more for unusual spectra. These categories were based on the strength of the emission lines of hydrogen; category A had the strongest lines, then B, and so on. This is an arbitrary choice, though a reasonable one, given that hydrogen makes up 70% of the matter in the universe.
This stood until 1901, when Annie Jump Cannon, an American astronomer, rearranged the lettered categories and dropped all the letters except O, B, A, F, G, K, and M. (She also came up with the famous mnemonic in the opening paragraph.) This re-sequencing of the letter categories worked because it followed what the spectra were doing – a sequence of spectra according to Williamina's original scheme of A–N would result in the hydrogen emission lines smoothly varying from bright to faint, but other spectral lines wouldn't necessarily follow a specific pattern. With Annie's re-organization, all the lines would vary smoothly (at least, more smoothly), although the theoretical work necessary to explain this wouldn't be fully completed until the 1920s.
Below, I've made a picture to help illustrate. In this picture we have some extremely stylized spectra, with a few fictitious emission lines at various wavelengths. For the purpose of this picture, hydrogen is the emission line in the yellow part of the spectrum (although in reality hydrogen has one red line and several blue ones as seen in the Balmer Series). The left side has the spectra organized according to Williamina's original sequence, while the right side has Annie's re-organized sequence. If you watch the yellow line on the left side of the graph you'll see it smoothly varying from bright to faint as you go down. On the right, all the lines are smoothly varying, but not just from bright to faint; some go from faint to bright, while others go up and then back down.
Spectra fit nicely into Annie's scheme, but it wasn't apparent why until the Indian physicist Meghnad Saha derived a theory of ionization in the 1920s which the British-American astronomer and astrophysicist Cecilia Payne-Gaposchkin used in her doctoral dissertation to show that the sequence of spectra was actually a sequence of surface temperature. (This has been called “the most important dissertation in the history of astronomy.”) O-type stars have the hottest surface temperatures, while M-type stars have the coolest (our Sun is a G-type star, for reference).
This is an important point, and one that requires quantum mechanics to appreciate fully. Atoms emit photons of light when electrons in them drop down from a high-energy state to a low-energy state, the exact states determining the wavelength (or color) of the light. Without an external source of energy, electrons will sit around in the lowest energy state they can reach; the continuing emission of light from stars comes from the heat of the star constantly exciting electrons into higher-energy states.
Pivotally, the efficiency of this process depends on the exact temperature. Put simply, too cold and the atoms of a given element (hydrogen, for this example) won't get excited very often and thus won't put out too much light. Raise the temperature and you should get more and more excitation and the brighter light, right? This is true, but only up to a point. Once a certain key temperature is reached the emission of light will actually begin to drop off again. The reason for this is that higher temperatures actually make the electrons too excited to drop down to lower-energy states easily (or at least, make the transitions that correspond to visible light; in reality, they'll be making other transitions that correspond to non-visible light).
It turns out that A-type stars are ones where the temperature is just right for exciting hydrogen, thus giving them the brightest hydrogen lines. As you go away from A-class the strength of the hydrogen lines drop off as the stars become either too hot or too cold. And that's where that odd sequence of letters comes from!
Addendum: In reality, there's a bit more to classifying spectra than just these letters. In practice each letter class is split into ten subclasses using the Arabic numerals 0–9 (a practice also started by Annie Jump Cannon), and also a luminosity class denoted by either Roman numerals or additional letters for special cases. For instance, our Sun is classified as G2V, indicating a main-sequence star with a surface temperature of about 5,800 K.