Seeing stars is usually due to a lack of blood reaching the brain, and thus a lack of oxygen. Most commonly this happens after standing up quickly or straightening after bending over. You don't see it all the time because the arteries serving the brain reflexively dilate to maintain pressure, but those reflexes may be disturbed by lack of sleep or food, a hangover, or an infection. Physical trauma may also cause the reflex to malfunction, which is why stars float around Sylvester the Cat's head when he gets bonked. Rarely, seeing stars can be caused by too much blood reaching the brain. This doesn't happen much outside of the laboratory or a jet fighter, as the arteries have a very strong constriction reflex to keep it in check.
The stars you see are actually neurons in your visual cortex misfiring, a hallucination at the lowest possible level. When the neurons' oxygenation changes drastically, their membrane potential also changes. Ordinarily this wouldn't matter, because all of the surrounding neurons' potentials would change at the same time. When it happens due to standing quickly or taking a few G's of acceleration, the change happens so fast that the neurons closest to capillaries change well before the surrounding neurons. This causes them to fire spontaneously which your brain interprets as vision; you see stars.
Another possible cause of seeing stars is posterior vitreous detachment, which mostly affects people over age 50. PVD happens when the eye's vitreous shrinks and collapses, detaching it from the inner surface of the eye. This causes all manners of floaters and distortions to be visible, including ones that look just like the stars mentioned above. PVD can result in retinal breakage or detachment and thus cause blindness, so if you notice a sudden onset of seeing stars and other visual phenomena you should probably go see your doctor.