Preparing Walls & Wood
Before painting or staining wood trim, it should be prepared to accept new finish. On new wood, that won't involve much more than filling in exposed nail holes and lightly sanding the wood.
If you're repainting trim that's now covered with paint, it may be enough to wash it with a cleaner like TSP (trisodium phosphate), then lightly sand it. But if there's layer upon layer of old paint built up on the trim, you may choose to strip those off before applying a new finish.
Cleaning Old Paint
If you're painting over an old coat of paint that's in relatively good shape, it's fairly simple to get it ready.
The first step is to wash it with a cleaning agent like TSP (trisodium phosphate) which removes the shiny surface coating on gloss and semi-gloss paints.
New paint won't bond as well on a glossy surface. So after washing, lightly sand away any remaining gloss and rough up the surface a bit for the new paint to adhere.
Repairing Damaged Wood
There are wood patching products you can use to fill holes or gouges in wood trim. If you're painting the trim, any of the gray or white latex products will work fine.
If you're staining the wood, use a filler tinted to the color of your stain. Use a putty knife to apply it and to scrape away the excess. Allow the filler to dry, then sand the surface smooth.
Sanding Before Finishing
It's generally a good idea to sand any bare wood before finishing it, but be careful with a wood surface you've just stripped if you're planning to stain it and give it a clear finish.
Sanding can remove the effects of aging or the "patina" which you may actually want to preserve. And uneven sanding on older wood can produce discoloration. In that instance, you might want to just skip the sanding process altogether.