The most basic door frame is simply framed into an existing pattern of two-by-fours and supplemented with some hardware. Here are some steps to build a door frame.
Measure your door. Know what your dimensions are before you start. Then add an inch or two each way.
Set up two parallel studs with a width space, and then cut them for height and add a cross beam to join them at the top. Then add a door jamb, a panel on the hinge side, that you will hang your door on. With a few additional modifications, you have your doorway.
Mark where hinges will go. Also mark where the knob/tab mechanism will go on the other side of the doorway.
Adjust for plumb. Use a level to see if your doorway is plumb. If not, use wooden shims to set it right.
Add interior molding to the door frame. Leave the edges free so that the door will swing either outward or inward.
Attach the hinges to your right side of the frame and hang the door. Adjust for plumb again, as necessary.
Attach the knob/tab mechanism. Make sure it is set at the right height, so that the retractable tab on the door clicks into place in the setting (attached to the frame) when you close the door. Alternately, if you have a latch mechanism, make sure the door is not dragging or hanging diagonally on the hinges.
There are several ways to adjust a door to get its level right. One common way is to wedge thin materials in between the door jamb and the frame until the door is at the proper place.