"Why" is a good question, and one that science has yet to fully answer. We generally have a good understanding of "how" things work at the subatomic level, based on over a century of observation followed by theory and math backed up by experimentation. However, the really basic, naively simple questions, like "where do particles get the charges, spins and masses that define them" are the questions scientists are asking now.
As for the discrete quantum energy states theorized by quantum theory, those are the result of a lot of math that explains what we see much to well to be coincidence. Basically, an electron can be thought of as orbiting its nucleus, but it's really more like a vibration. Similar to a vibration, there are frequencies above the "fundamental" that produce "harmonics"; standing waves with wavelengths of a perfect integer fraction of the fundamental, and perfect integer multiples of frequency, which are self-stabilizing. Electrons can only exist in these discrete energy states because if they didn't, they'd "break orbit" and go zooming off as free radicals, or have an unstable or unenergetic path that sends them crashing into the nucleus.
What's weird is that that doesn't ever happen as far as we know; not only do electrons exist in stable states, they only exist in these states, and we can't get them to exist in any energy state between two discrete states.
Max Planck first posited these quantum energy states existed while attempting to solve a conundrum of classical physics. Back to the vibration analogy, consider a guitar string. By damping the string at precise points on the string, we can induce clear harmonics. however, the higher the harmonic, the harder you have to pluck to produce a wave of the same amplitude. As the wavelengths get smaller, the energy you have to put into them to obtain a constant amplitude (and thus the energy inherent in the wave; Newton's third law) increases exponentially, diverging to infinity as the wavelengths approach zero. Now, this was obviously wrong; observed visible spectra of a glowing piece of metal as it is heated clearly shows a "peak" in the frequency of light produced, and at frequencies above this peak, amplitude of emitted light drops off sharply.
Planck solved this "ultraviolet catastrophe" by describing what was going on as similar to this string having a number of springs of different length attached to it, with weights on the other end of each spring. As the string vibrated, it in turn induced vibrations in the springs (known technically as "harmonic oscillators"), and as the frequency increases, it approaches, matches, and then falls away from the harmonic frequencies of the springs, increasing their relative amplitudes and then making them fall off as the vibrations begin to cancel out, while simultaneously increasing the energy imparted to a different string whose harmonic was being excited. The relative harmonic frequencies of the springs are what determine the colors of light seen, and the total energy of the string is divided among the springs, keeping the same total energy as frequency increases instead of diverging.
To make the math work, Planck had to describe the springs as each receiving an integer portion of the energy of the entire system, and described these energy levels as proportional to the length of the spring. The proportionality is defined by a slope known today as Planck's Constant. Thus, Planck asserted that all energy existed in discrete amounts, which he called "quanta", hence "quantum theory".
While this worked, it seemed to do so by convenience, until Einstein then further developed photon theory: light was made of of photons, "packets" of energy that could be quantified in proportion to the photon's frequency, based on Planck's Constant. This in turn explained puzzling properties of the "photoelectric effect", namely that the threshold where electrons began to be ejected from metal when exposed to light (forming an electrical charge) was dependent not on intensity of light, but on the frequency of the light. Einstein's photons explained that, because an electron was likely to only be hit by a single photon of light, no matter how many there were, the energy imparted to the electron could only come from that single photon, and as a photon's energy increases with frequency, there is a minimum frequency required to impart enough energy with a single photon to eject an electron.
Planck's constant, and the highly definite nature of how atoms interact with photons, continues to show up in math that accurately describes things we observe about matter, such as how materials emit very specific wavelenghs of light while absorbing all others. That was explained by the Bohr model of the atom, which was the first model to introduce quantum theory to atomic structure. The Bohr model was based partially on a mathematical model first proposed by Johann Balmer and then improved on by Johannes Rydberg, showing that the emission spectra of hydrogen was predictable by an equation with integer solutions. Bohr showed that the energy states of electrons, which can be altered by absorption of photons, must exist in certain integral states, defined by an equation derived from Rydberg's but that also includes, you guessed it, Planck's Constant. The famous "double-slit experiment" showing that light is both wave and particle, and the resulting mathematical models by Louis de Broglie showing the movement of electrons as a series of stable standing waves of vibration in the electromagnetic field of the nucleus (electrons, in fact, are similar to photons in that they are as much a wave as a particle), reproduced the hypothetical energy states of Bohr's model by a different method, and in math, when you get independent confirmation of your results by a completely different mathematical model, you know you're on to something.
Now, that's the theory to date (well, actually I stopped about 100 years' worth of research ago, but them's the basics of the discovery of discrete quanta). What we are still trying to explain is why these quanta actually exist. A spaceship can have an infinite number of stable orbits around the Earth, and there are countless stable orbits of planets and planetoids around the Sun; why then does an electron stabilize so deterministically in these discrete states? Our best answer is that the electron's wave motion is only stable in these discrete states; otherwise the wave would interfere with itself or others and cancel out. However, we have never seen this "turbulence" happen and can't seem to force it to happen; it seems to be a fundamental quality of matter.