The back of the eye contains millions of special nerve cells that detect light and send signals to the brain telling it of the presence of the light. When the head is struck, these nerve cells are jolted, causing them to send erroneous signals. When the signals are received by the brain, they are interpreted as tiny points of light, causing the person struck to think he is seeing stars.
From: More Ever Wonder Why? by: Douglas B. Smith