If you've got the installer disc for Mac OS X, load the disc into your Mac and boot up or restart; hold down C as it boots and you'll be taken to the Mac OS X installer.
(A quick note - when testing out these methods on a MacBook we occasionally found that the USB keyboard wasn't working during boot-up. It should be fine, but if you're holding down the keys to activate Recovery mode - or, in later methods, Single User Mode or whatever - and it isn't working, one quick fix to try is to use the keys on the laptop itself.)
These days, however, very few people have got or can use the installer CD for the version of OS X they're currently using - we tend to download and update OS X from the App Store, and most recent Mac models haven't even got a CD drive. In this case, things are a little more complicated.
If you've got a recovery partition set up (here's how to create a recovery partition so you're covered in future, but that won't help you now), you can hold down the Option key (also known as Alt) while booting and choose the recovery partition at startup.
If you haven't got a recovery partition, you can load the installer file for your chosen version of OS X on to a flash drive. Unless you set this up already, you'll need a second Mac that you have got the password for to get hold of the installer file. Here'show to create an emergency Mac OS X boot disk from a USB flash drive. You can then boot from that.
However you get to the Mac OS X installer, you'll now be able to change the Mac's password. If you're on OS X 10.7 Lion or earlier, select Utilities (from the menu bar), then Password Reset. You'll need to choose the drive on which OS X is installed, and the user whose password you want to change; then type in a new password and try to remember it this time. If you're on OS X 10.8 Mountain Lion or later, select Terminal, type resetpassword and hit Enter, and again you'll be able to type in a new password.
How to hack into a Mac without the password: Reset the password in Single User Mode
If for whatever reason you can't or don't want to access the OS X installer, another option is to use Single User Mode and once again change the password you can't remember to something you can. This will involve a bit more typing.
Sign up for CIO Asia eNewsletters.