Chai, If you leave grains, cereal or flour around too long the ever present larvae hatch and you get moths. Once you have them flying around they can be difficult to eliminate. Yes, we are all consuming moth larvae with our Raisin Bran et al, so take that all you vegans.
If you have moths, you must keep everything...especially flour, pasta, cereals & breads...in airtight containers. I keep flour inside a ziplock bag inside a canister. Ziplock bags work great.
They make sticky traps for pantry moths that are very effective, just Google for a source. As Eva says, put everything in tight closing containers. You should also rotate your dry goods so nothing sits too long. I don't recommend moth balls, even the fumes, are poisonous to pets and especially children.
The best chemical free way to keep one's house moth free is move in a family of brown bats in your pantry. Make sure you don't buy them at Walmart. They're extra cheap but those Made in China bats tend to carry rabies.