Measure the area where you want your closet to go. Use a stud finder to mark the studs in the existing wall nearest the points where you want the walls to go. Each closet wall needs to attach to an existing stud -- so stud placement will ultimately determine the size of your closet. Measure from the floor to the ceiling, then subtract 4 inches from that dimension to determine the height of the closet wall studs. Measure the frame of the pre-hung door to determine the width and the height of the door. Measure the depth of the closet to find how long you need the side walls to go. If the closet is in the middle of the room you will need two side walls and a front wall. If the closet is in the corner, you will need one side wall and the front wall.
Cut the 2-by-4s with a circular saw to make the pieces for the wall frames. For each wall frame, you will need two pieces that are the same length as the closet's wall. You will need one stud at each end of each of your closet walls and one every 16 inches along the length of each wall, and one piece that is as long as the door frame is wide.
Assemble the wall frames by laying out the four edges of each frame. While the pieces are on the floor they'll sit on their narrow sides so that, when you raise the wall, the top and bottom plates' wider faces are against the floor and the ceiling. Use two nails at each end of each stud to nail them into place. Once you have built the outside pieces of the frame, make sure it is square using a T-square; then nail the studs in every 16 inches down the length of the frame. When you build the front wall, instead of placing studs within the space where the door goes, put one stud on either side of the door's place so you can just slide the pre-hung door into place later. Nail the piece that is the length of the door at the height of the door frame.
Pull the carpet and the pad back where the closet will be. Just fold it back, because you can put it back into place and cut out the wall pieces when you are finished.
Raise the wall frames. Nail them into place along the studs you have marked, placing a nail every 12 inches. Then nail the new wall frames into the floor on either side of the studs, and nail the frames into the ceiling or into a ceiling joist if possible. Nail the wall frames together at the corners where they meet.
Hang drywall on the outside of the closet wall frames. You need to hang drywall on both the interior and exterior walls of the closet. Use screws to hang the drywall, and screw it into each stud along the top and the bottom of the wall. Cover the screws with drywall compound, and fill in the seams between drywall pieces with drywall tape and compound. Allow the compound to dry, then sand it and paint it.
Install the door by sliding the frame into the space you made. Make sure the door swings freely, and use a mallet to bang the door into place, until it does swing correctly. Then nail the doorframe into place every 18 inches along each wall. Put in baseboards along the bottom of the walls, and trim around the doorframe.
Roll the carpet back into place. Use an X-Acto knife to cut out the portion of carpet that would situate under the new closet walls. Glue it down. Push the edges of the carpet underneath the baseboards.