Hold a length of straight PVC to the bottom of the drain stub and hold the p-trap to the other end of it. Determine if the other end of the p-trap is at the same height as the drain stack in the wall. If the p-trap is too low, cut off the end of the straight pipe with a hacksaw to make the p-trap hang at the correct height.
Fit a plastic PVC nut on one end of the straight piece with the threads pointing to the end of the pipe. Place a plastic gasket over the end of the pipe with the flanged part pointing toward the end of the pipe. Do the same on the other end.
Insert one end of the straight pipe into the end of the drain stub and tighten the plastic nut by hand onto the threads of the stub.
Connect the long end of the p-trap to the other end of the straight pipe and tighten the plastic nut by hand to the threads on the p-trap.
Cut the second pipe piece to fit between the other end of the p-trap and the drain stack if it is not the correct size. Place a nut and gasket on each end of the pipe as you did previously.
Attach one end of the pipe to the open end of the p-trap and the other end to the drain stack pipe. Tighten the nuts by hand. Tighten all the nuts half a turn with a pair of slip joint pliers.