No carpet is stain proof, but many are stain resistant, and so you have time to act.
It is very important to treat your specific spill as quickly, and accurately as possible. For example, water-based substances will require a different cleaning method than oil-based substances. Our link has provided cleaning methods for over forty potential spills: be sure to select the one that most closely matches your stain.
Remove as much of food spills as possible by scraping the carpet gently with a spoon or a dull knife.
Absorb wet spills quickly by blotting repeatedly with white paper towels or white cloth towels. Always blot; never rub or scrub abrasively, as a fuzzy area may result. When blotting, work from the outer edge in toward the center of the spot to avoid spreading the spill.
Remove the stain using one of the cleaning items from the link.
Rinse the cleaned carpet area with water to remove detergent residue that may become sticky and cause rapid re-soiling.
Absorb any remaining moisture by placing several layers of white towels over the spot and weighing them down with a heavy object. This step is necessary even when the carpet doesn't seem particularly damp.