Docking was officially introduced in KSP 0.18 and allows separate ships to join into a single entity that is controlled as one and allows the transfer of resources (but not yet Kerbanauts) between connected modules.
To get close enough to even consider docking, you'll need to perform an orbital rendezvous.
Using the docking controls
The faintly purple button around the bottom left corner of the screen (just below the green staging button, and above the blue map button) sets your ships into docking mode. In docking mode your main engines are shut off and your normal WASD controls map to RCS translation maneuvers instead.
To dock with a nearby object (see the basic maneuvers page for how to rendezvous with another object in orbit), start by rotating your ships so their docking ports are facing each-other. Remember to toggle any docking-port covers if necessary. Also keep an eye out for how your ships will connect and make sure you won't run into solar panels or other modules on your approach.
Right click the docking port on your ship and hit "control from here". Now activate docking mode. What is happening now is that docking port will be seen as the "center" of your ship. The controls will be very similar to the EVA controls. W/S will be used to move forward and backwards, A/D will be used to move left and right, Shift and Control will be used to move up and down.
Once your docking ports are very close (or touching) they will magnetically clamp to each-other and you will have successfully docked. The tolerance for the clamps is quite generous, however, it can take some time for it to kick in - so once you get your ports in close proximity, just wait a bit before trying to get any closer (and possibly damage your ships). If your ports stick together, but don't actually dock, try turning the SAS off: it might be preventing your ship from turning a crucial few degrees.
Note that you don't have to be in docking mode to dock - if you manage to align your docking ports using the regular controls, everything will still work.
While docked, you can transfer resources (fuel, etc) by clicking on a tank, and then alt+right clicking on another target and pressing the desired In/Out switches of the windows that pop up.
As of 0.23, Kerbanauts have to be transferred manually using EVAs.
To undock simply right click either of the docking clamps and click undock. In order to facilitate separation, the magnetic clamping feature is turned off when undocking is executed. Once the ships have separated (about 5-10m) the magnetic clamping feature will automatically reset.
There are five docking ports available for different sizes and docking directions. In order to dock two spacecrafts, both must be equipped with docking ports of equal size. When constructing a spacecraft, another part which is not a docking port can be placed on it. That part can then be undocked, but not redocked.
- Clamp-O-Tron Jr. is the tiny port.
- Clamp-O-Tron Docking Port is the small port.
- Clamp-O-Tron Sr. Docking Port is the large port.
- Clamp-O-Tron Shielded Docking Port is a small port (compatible with the Clamp-O-Tron Docking Port) covered by a retractable shield. This is meant to protect the port from atmospheric friction, but KSP does not implement the damaging effects of that friction as of 0.23. The port's cover must be opened before it can be used for docking.
- Inline Clamp-O-Tron is a small port mounted within a recessed bay in a cylindrical fuselage. This allows the port to be hidden within a stack of small parts. The port's cover must be opened before it can be used for docking.
Unfortunately, docking ports are not very rigid. This makes crafts which consist of multiple large sections very difficult to control. To mitigate this problem, you can use bi- or tri-couplers to mount multiple docking ports. The resulting multi-part connection is a lot more stable. Unfortunately docking these crafts is even more difficult than docking with a single-port connection, because now you also have to take care of the rotation of your craft. Otherwise it can happen that only one port connects. Should this happen, undock your craft and try again. Sometimes when using tri-couplers, it can happen that even though the orientation is perfect, only two ports are actually connected. You can tell by right-clicking the ports. When none of two ports has an "undock" option, they are not actually connected. This problem can be solved by going to the space center and reloading the ship.
Additionally, it is recommended to control a multi-part craft from the command pod closest to the center of mass of the whole contraption. In that case the direction on the navball is affected the least by the ends of the craft wobbling around.
Alternatively, since version 0.20, Clamp-O-Tron Sr. Docking Ports have become a better way of keeping two large ships together than triple docking small Clamp-O-Tron Docking Ports, seeing as they cover the same area, but only need one attempt to completely dock together. However, docking together several very, VERY large ships together will require using several Clamp-O-Tron Sr. Docking Port anyway.
It is possible to establish a multi-port connection without using a multi-coupler, but great care must be taken in the VAB when manually placing ports for this purpose.
Possibilities for docking
Docking gives you some great new abilities.
In the situation that you have a large rocket, such as an interplanetary rocket, you may find yourself running out of fuel before leaving Kerbin's sphere of influence. When this happens, all you have to do is send some tankers to orbit, dock, transfer fuel from the tanker to the rocket, and be on your way. In order to start the refueling you have to press Mod Key + right click the two tanks that should send/receive fuel, and click on the appropriate panel that appears.
If you need to refuel frequently, you may consider building a large orbital tanker, or a space station with one.
In-orbit spacecraft assembly
There is a limit of how large rockets can be built in the Vehicle Assembly Building. What does one do to fix this? Dock multiple crafts together to create a larger rocket! The cancelled Constellation program by NASA was planning to use this method for manned missions to the Moon and Mars.
Note though that currently (as of 0.21.1) docking connectors are a bit wobbly, so ships from too many too heavy components will be hard to control. Docking ports can even rip off when put under too much stress. This problem can be mitigated by using multiple docking ports on bi- or tri-couplers (see above). A mod which offers a different workaround for this problem is the Quantum Strut mod. Or, one can use time warp to force-stop all rotation, wobbling, and such. Note, this may not work every time.
Detachable landing crafts
This method was used during the human moon landings , and it can be equally useful for the Kerbal. When you do a landing mission and have the intention of getting back to Kerbin, don't take all the fuel for the return flight to the surface of whatever planet you are visiting. Undock a lander with just enough fuel to get to the surface and back, and redock it with the return stage when it's back in orbit. You can save a lot of fuel that way, because the lander doesn't need to carry all the return fuel to the surface and back into orbit.
Small in-game computer systems
Docking Ports can also be used to create limited orbital computers. 
- Basic Docking tutorial