Many stains are on the surface of the grout and can be removed with sandpaper. Just fold the sandpaper into a crease, and work it back and forth along the grout line. If sandpaper doesn't work, you may have better results by applying a pencil eraser.
If sandpaper or an eraser fails to remove the stains and dirt, try an over-the-counter grout cleaner -- or create a similar solution from household bleach and water. Squirt or pour the solution onto the grout, and scrub with a toothbrush. Make sure the area is well ventilated, and wear rubber or latex gloves when working with bleach.
If bleach doesn't do the job, you may need to remove and replace the grout. Use a grout-removal tool or a rotary tool with a carbide tip (Image 1). After you install new grout, seal it with a grout sealer (Image 2) to reduce the likelihood of future stains setting in. Reapply sealer once every two years.