Keep your locos in good check. First, remove the cover. If it's steam, there's likely a screw in the smokestack and/or underneath. If it's diesel, spread the body at the sides to release the pins and pull upward. Now- flip the loco upside down and clean the wheels. I like to use an old X-acto blade to peel off gunk, and usually finish up with an eraser-pad. Also- be sure to oil the loco's moving parts as the instructions say to. Use a thin oil, such as 3-in-1. Be careful not to over-oil, as excess oil can drip out. This not only makes a mess, it also inhibits electrical conductivity if it lands on your rails!
Occasionally (annually) you may need to clean the commutator and brushes (where the motor picks up electricity). I like to run the loco slowly without the body on and keep a rag with cleaner (Rail-Zip works well) over the commutator as it spins. You may need to get a can of tv tuner cleaner (sold at electronic stores) and spray some on the brushes. Let it drip off, and wipe the area clean and dry. Put the body back on.
So the rails shine and the locos run, but it all messes up again quickly? Maybe you need to clean your freight car's wheels. The easiest method is to mount a straight section of track on a 2x4 scrap- about 18 inches long is excellent. Put a paper towel or rag over the rails heads, and then drip on some rubbing alcohol. Next, dribble on some Goo-Gone, by name- a citrus miracle fluid. Run the cars back and forth a couple times, and check the progress. The wheels will look shiny and new, and your rag will have dirty stripes on it. Change spots on the rag every so often.
If you rolling stock derails often, check their wheelsets against an NMRA standards gauge. While adjusting the problem can be difficult if you have plastic wheelsets, metal ones often allow easy adjustment. This usually isn't a problem with locomotives, although occasionally wheels can get out of place.