Use a Sharp Saw Blade
You can't cut perfect miters with a dull blade, one with too few teeth or one that's designed for ripping. Check your blade for sharpness by cutting a 45-degree miter on a 1x3 or larger piece of oak or other hardwood. If the blade cuts smoothly with very little pressure and leaves a clean, almost shiny cut with no burn marks, it's sharp enough to cut good miters. When you check your blade or shop for a new one, look for one labeled as a “trim” or “fine crosscutting” blade. A 10-in. blade should have at least 40 teeth, a 12-in. blade at least 60. If the blade is for a sliding miter saw, be sure the teeth have a hook angle of zero to negative five degrees. Teeth with a neutral or negative hook angle are less aggressive and safer for sliding miter saws. Expect to spend at least $50 for a carbide-tipped blade that'll perform well and last.
Tweak the Cut
Even on perfectly square corners, 45-degree angles won't always yield perfect miters. Wall corners can be built up with corner bead and compound, and window and door frames can slightly protrude or be recessed behind surrounding drywall. That's when you have to start fiddling with the angles to get a tight fit. In most cases, you'll be making adjustments as small as a quarter of a degree. If the gap is small (about 1/16 in.), recut one side of the miter. If the gap is larger, you'll have to recut both boards or the trim profiles wont line up.
Glue and Sand for a Seamless Fit
Here's a trick to make miters look great, but it only works if you're installing raw trim that will get finished after installation. Apply a thin layer of wood glue to the end grain of each piece before you assemble them. Use a damp (not wet) cloth to remove excess glue from the joint. Sand over the miter with a small piece of 120-grit sandpaper. Sand across the joint and finish up by carefully sanding out any cross-grain sanding marks by moving the paper with the grain from both directions.The sawdust from sanding will mix with the glue to fill any small gaps. Sanding the miter will also even out any slight level differences and make the job look more professional.