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.
Try out the table saw miter sled in the video below to get started.
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.