Clean stains ASAP
Cleaning any spills as soon as possible is vital if you want to avoid staining on you carpet. Occasionally an old stain will come out with some effort, but you have a much better chance of success if you get to the problem straight away.
Spilled substances can be corrosive to the carpet fibres, so the faster you get the substance cleaned up the better. Else, a spilled substance can chemically react with carpet fibres or dye, causing discolouration. And even if the surface of a carpet is not affected it is possible that a liquid spill will soak into the underlay and cause moisture problems, like mould or mildew.
Test All Cleaning Products Before Use.
It is reasonably easy to make a cleaning product that gets rid of everything. But these products tend to turn all carpet fibres white; they remove the colour along with the stain. A carpet offcut or closet corner is ideal for testing cleaning products. Make sure they make the carpet look like new, not like the components they were made from!
Heat on Stains
Heat will cause most stains to set further into the carpet. Even the heat from steam cleaning is strong enough to cause this. Occasionally some crayons can be ironed off a material surface (Google for this trick); otherwise soak up spills with white paper towels and use a vinegar/water solution to dab the carpet with. Some absorption powders (i.e.: Karpet) work well for some stains.
Not Getting a Periodic Professional Cleaning
Getting a professional clean every 6 to 12 months massively extends the life of carpet. By removing the in-ground dirt there is less much wear on the carpet. It aloe removed toxins, mould and other issues. Clean carpet is less prone to stains from spills. Water split on a clean carpet is a moisture issue. Water spilt on a dirt carpet creates mud, which is much worse.