In fact, you don’t need to do anything to lock the iPhone; it happens automatically, if you don't touch the screen for one minute.
Can’t wait that long? To lock the iPhone immediately, press the Sleep/Wake button. To unlock it, press the Sleep/Wake button again. Or, press the Home button on the front of the screen. Either way, the on-screen slider appears, but your iPhone doesn’t actually awaken until you drag the slider to the right with your finger.