There are both physics and biology at work here.
Basic physical properties of light
The first thing to understand is that light has a property known as “wavelength,” since light is an electromagnetic wave. The wavelength is the distance between two crests of that EM wave. As you might imagine, the distance is extremely small, usually measured in nanometers, at least for visible light (more on what makes it visible in a moment).
Basic biology of vision
The human eye, meanwhile, has special cells called “photoreceptors,” which are sensitive to light and trigger nerve cells to send a signal to the brain when light hits them. More light on them, stronger signal (simplifying a little here, for the record). However, photoreceptors are only sensitive to light with certain wavelengths. If light has a wavelength that none of our photoreceptors are sensitive to, we can’t see it—so visible light is just the light that we do have photoreceptors for.
Visible light spectrum
This graph shows the various types of light we recognize based on its wavelength, with the wavelengths that are visible to typical humans highlighted:
(Wikipedia EM Spectrum)
The important thing you want to notice here is that yellow is in between red and green. That is a key part of why mixing red and green produces yellow, but it’s not the full story. It’s the physical reality that our biology is trying to tell us about, but how our biology does that plays a bigger role.
Biology of color vision
Human photoreceptors are broken up into two primary categories—rods and cones—and then the cones, which handle color, (usually1) come in three varieties: those most sensitive to red wavelengths, those most sensitive to green wavelengths, and those most sensitive to blue wavelengths. Hence RGB. We see something as red because when the red light hits our photoreceptors, the red-sensitive cones are the ones that activate most. Same with green light activating our green cones.
It’s important to note that the cones’ sensitivity isn’t sharply defined; instead, they are just most sensitive to some color, and then get progressively less sensitive as the wavelength moves away from that color. And the sensitivities of the different cones overlap. So even with green light, your blue cones and red cones are still activating—just not as strongly as the green ones.
Here’s a diagram of a typical human eye’s photoreceptor sensitivities:
(Wikipedia Color Sensitivity)
Yellow light, or red and green light?
And that is how the eye can give the brain information about light that isn’t red, green, or blue: if yellow light hits the eye, the red cones and the green cones will both be activated. The brain gets the signals from the red cones and green cones (and the lack of, or weaker, signal from the blue cones), and interprets that as “yellow,” that is, light with a wavelength in between the peak sensitivities of the red and green cones.
But the only information the brain really gets is that the red and green cones are activated. That could be because of yellow light, but it also could just be red and green light hitting the eye at the same time. The brain does not have the information it would need to know the difference, and so it just treats those two situations the same—as what we call “yellow.” That’s why you can output red and green (and not blue), and have the eye see yellow without having to actually have a yellow light source. And the eye does this with all colors; because the cones’ sensitivities overlap, there is always some kind of mix of signals that the brain combines into a single color, usually something like the “average” among them.
Red and blue light—definitely not green
An important exception to that “averaging” (which isn’t strictly an average, mathematically speaking) is when you have red and blue cones activated, but green cones not (as strongly) activated. Unlike the situation with yellow—where the brain had no information about whether it was seeing yellow light or a combination of red and green light—the brain does have information telling it that the green light isn’t present, because the green cones aren’t as strongly activated. So “averaging” the red and blue to make green would be really wrong—that’s the one color the brain knows isn’t there.
Instead, the brain perceives the combination of red and blue as magenta, a color that does not exist on the actual EM spectrum. No single wavelength of light appears magenta to us: only the combination of blue and red lights can cause us to perceive that color.
No, RGB isn’t universal.
First, light sources combine “additively,” that is, if you take some light and add a new light of a different wavelength, the new wavelength is added to the combination.
Dyes, though, combine “negatively,” that is, when you mix dyes together, you are removing more wavelengths from it. The reason for this is that dye absorbs some light and reflects others—white light is how we perceive a mix of all the wavelenths we can see, so if white light hits red paint, the blue and green wavelengths get removed and only red is reflected back at our eye. That’s why the primary colors you learned in grade school are red, blue, and yellow,2 with green formed by mixing blue and yellow together. It’s also why printers prefer to use CMYK over RGB: Cyan-Magenta-Yellow is a better place to start removing wavelengths than Red-Green-Blue are (Black is handled separately just because black is particularly important in printing and you want to separately make a really good black rather than trying to use all your other inks trying, and failing, to remove all wavelengths).
There are also other approaches to handling light, that don’t directly have anything to do with wavelengths, but rather more based on how you want the light perceived. Hue, Saturation, and Lightness, for example, will produce colors of a some wavelength or combination of wavelengths, but the numbers aren’t the intensity of lights of different wavelengths the way they are for RGB or CMYK.
Finally, none of these actually covers the entire spectrum of colors that the human eye can see. That’s because natural light covers a continuous spectrum of wavelengths, that is, the number of wavelengths in, say, sunlight is literally uncountable,3 and our photoreceptors are still somewhat sensitive to the colors around their peaks so our eyes can pick up on some of those wavelengths. RGB specifies combining just three wavelengths at different intensities, and there are simply always going to be colors you can’t make with just three wavelengths. You could add more wavelengths, but that means more independent light sources, and you certainly aren’t ever going to have infinitely-many of them. But three is pretty good; the four-color TVs didn’t really take off for precisely that reason.
This diagram shows the colors you can make with a typical RGB set-up, with the large gray area around it all the colors you can’t make.
(Wikipedia sRGB Gamut)
Note that the arc along the top is the spectrum of monochromatic colors, that is, the light made up of just one wavelength—the spectrums in the above diagrams would be wrapped around that curve. And magenta forms much of the line connecting the bottom two ends of the curve.
Color blindness occurs when some of those cone cells don’t work, or at least don’t work well. There have also been a few reports of people with four types of cones. And other species can have entirely different sets of photoreceptors with entirely different sensitivities, allowing them to perceive more colors that would look the same to us, and also allowing them to perceive light that is simply invisible to us.
If, like me, you learned the primary colors with some kind of paint, that mixing is actually more complicated than just being straight “negative,” but for grade-school children it’s good enough. For this answer, I’ll stick to dyes, which are physically closest to just being the simple negative case.
Practically speaking, anyway. Quantum mechanics might suggest that all light has a wavelength that is a multiple of some incredibly small distance, possibly the Planck length, but this isn’t anything anyone’s really nailed down a theory for, much less shown it experimentally.