Reaching to open a door and having the knob actually come off in your hand is like a scene out of a silent movie, but it's something many of us have experienced. When this happens, your first thought might be, "I wish someone were here to see this!" while your second thought is likely be more practical: "Hey, don't I need this to open the door?" You do, indeed, Mr. Chaplin. Here's a simple tutorial on how to fix that knob, so you can be out the door and on your way toward the next low-hanging obstruction or dropped banana peel.
First, some terminology…
The entirety of a doorknob mechanism is called a lockset. That includes the two knobs or handles; the latch assembly that inserts into the door's edge; and the bolt, the metal shaft that slides in and out of the latch assembly. The metal plate that mounts to the door frame is called the strike plate (because the bolt strikes it when you close the door). Doors that lock are properly called locksets, while doors that don't have locks, such as for closets, are called passage locksets or passage sets. This is good to know if you have to buy a replacement lockset.
Now, back to that loose knob…
Doorknobs can be attached in a several different ways. If the knob itself came off in your hand, it's probably the kind that slips over a cylindrical spindle, which is mounted to the door as part of the latch assembly. Look for a little slot on the door-end of the knob, then look for a small, matching button on the side or underside of the spindle. Slip the knob back onto the spindle, aligning the slot with the button. Depress the button with a flathead screwdriver so you can slide the knob over it, and push the knob in until the button pops out through the slot. This often requires a little fidgeting to get the slot in just the right place.
If your lockset has a squarish, solid-metal shaft instead of a spindle, the knob is held on by a setscrew, located on the door-end of the knob. Hopefully, your setscrew is still there or is hiding somewhere on the floor below. If not, you'll need to find an exact replacement (or at least a small machine bolt with perfectly matched threading). Loosen the setscrew just enough so that it's not penetrating into the knob's opening. Most setscrews use an Allen wrench, but some take a flathead screwdriver.
Some shafts are threaded and some aren't. With a threaded shaft, screw the knob onto the shaft until it is snug with the door, then back it off a bit to allow for smooth rotation. Tighten the setscrew while turning the knob back and forth slightly at first, to ensure the screw is bearing down on one of the flat sides of the shaft. With an unthreaded shaft, simply slide the knob onto the shaft and tighten the setscrew. With either type, you can play around with the position of the knob (retightening the setscrew each time) to get it as snug as possible to the door without having it bind.
For extra credit…
If you really want to impress yourself and/or your housemates, here's how to tighten a loose lockset: Look for screws on the plate or rim between the doorknob and the door; only one side will have them. See screws? Tighten them with a regular (flathead or Phillips) screwdriver, alternating between the screws as you snug them down to ensure even pressure. Be careful not to overtighten, which can bind the knob and bolt action (this also why you should use a regular screwdriver and not a power screwdriver or a drill).
Don't see screws? Remove the knob by depressing the button (described earlier) with a small flathead screwdriver or other pointy tool and pulling the knob off of the spindle. Pry off the decorative cover (the one between the doorknob and the door itself; called an escutcheon) with a flathead screwdriver. This should reveal a plate with two screws. Tighten these screws as described in the preceding paragraph, then reinstall the plate and knob.