For the door to swing and close properly, it is imperative that the wall opening framing is square and vertical. Place a level on the side framing to check if it is vertical. Measure across the two diagonals of the framing. If the framing is square, the measurements should be equal. If it is not correct, redo the framing.
With the framing square, it is time to address the hinges to make the door close and swing properly. One typical problem is that there is paint in the mortise in the frame and the hinge does not seat fully. When this happens, the door does not close centered in the frame, and it may rub or not close at all.
This situation is called a “proud hinge” because the hinge leaf extends out past the wood surface. Remove the hinge and clean out all the paint buildup. Be careful not to remove any of the wood, because a good fit of the hinge leaf in the mortise is important for stability.
Instead of removing the entire hinge or the door, just remove the screws from the proud hinge leaf and drive out the hinge pin. This allows you to remove the one leaf. This method comes in handy if you have installed a heavy solid-wood door for quietness.
Reinstall the hinge leaf and pin to see if the door fits better and swings freely. If you think you are getting close to the proper door positioning, remove the middle screw from each hinge.
Short screws are usually included with most doors. The screws reach only into the framing. Install a three-inch-long screw in the center hole of each hinge. This new screw should reach all the way into the wall framing lumber for secure attachment.
If the gap between the door and frame is not as even as you would like, adjust it by bending the hinge a little. Use a nail set or other hard metal piece as a wedge. Place it between the hinge leaves and all the way against the hinge pin eyelet. Close the door against the nailset until it begins to bend the leaves. Make very minimal adjustments to zero in on a good fit.
Another method is to put shims behind the hinge leaves to space them out to create the gap you desire. This takes more time than bending them, and the hinge will not be seated as securely in the mortise.
If things are really far off, you may have to drill some new holes to relocate the hinges. To do this, plug the old holes and start from scratch on the alignment. Always use a self-centering drill bit to make sure the drilled hole is centered under the hinge hole.