There are two different levels of stains: extrinsic and intrinsic.
Intrinsic staining is the result of all those varied influences that might occur during the tooth's formative years. Intrinsic staining also includes the yellowing of teeth that happens later in life, as we age. Along with wrinkled skin, sore backs and compromised eyesight, teeth just naturally yellow when we get older, although no one's sure exactly why. Intrinsic stains are much more difficult to remove.
Extrinsic staining happens when highly pigmented foods linger on the tooth for a lengthy period of time and ultimately find their way into the superficial layers of the enamel. These stains are much easier to manage than the intrinsic ones, and simply becoming aware of when you're eating or drinking staining foods is a great first step.
You probably already know what the highly pigmented staining culprits are: red wine, coffee and tea. But there are others that might not be as obvious, but are just as staining. These include blueberries, soy sauce, balsamic vinegar, tomato sauce and certain fruit juices (grape and cranberry).
It's pretty simple chemistry, teeth are whitened by hydrogen peroxide. When the hydrogen peroxide touches the tooth, a type of oxygen, called free radical oxygen, gets "excited", meaning that when it's activated, it revs up and dives through the tooth's enamel to its dentin, which is where the stains live. The oxygen hits the stains, breaks them up and wipes them off.