As with any good head injury, it starts with your brain.
The part of the brain that handles what you see is called the occipital lobe, located at the back of your head. Its job is to take the information sent from the retina and turn it into something that makes sense to you. So before you know that the thing in front of you is actually a fist heading for your face, your retina has to take the observable light, convert it first into a chemical signal, and then into an electrical impulse, before sending back to you brain for interpretation.
The occipital lobe will then say, yup, based on this information, that’s a fist. You get the idea: Your eyes and your brain work together to understand what’s in front of you.
Your brain reads other types of stimulation, too. For example, if you irritate the brain in some way, as scientists have found by literally poking it, it may create a response it is normally used to creating. A stimulus in the occipital region that is not actually anchored to any visual can then provoke a response it normally produces: light.
If you haven’t figured it out by now, the stimulus that causes these “stars” to appear is a firm knock to the noggin. However, not every time we hit our heads we see stars. This is for a good reason. When you hit your head on a counter while bending down to tie your shoes, for instance, your head stops suddenly, but your brain keeps moving. But because your brain is surrounded by cerebrospinal fluid, it’s cushioned from running into your skull. This fluid is what makes head banging and football possible without immediate brain injury. But when you or the thing hitting you moves at a high velocity, the situation changes.
It takes a pretty serious bump for the fluid cushioning system to fail to protect our brains. That baseball that zoomed through your hands and slammed into your forehead forces your skull to snap back, which can cause it to smack against the front part of the brain. Then, when you fall and hit the back of your head against the ground, your occipital lobe continues downward and can hit your skull. Voila: stars.
But no worries, there are less dramatic ways to get this effect. When you rub your eyes in the morning, you can see stars, too. In effect you are manually creating the same stimulus that the retina would when it sees light. This is because the retina only really knows how to do one thing; it either sees light or it doesn’t. So when you apply pressure, you can basically make it think that the switch has been flipped. The duped retina will then send your brain something like an email with the subject line “light!” with no text in the body. So that’s what you get: flecks of uninformed light on top of the landscape that your brain can make sense of.
[Additional information here]