If you forget your device passcode or PIN, there is no way to get into it again. You have to do a factory reset of the device. For that, you need iTunes and a USB cable to connect your device to your PC.
Find below the steps to recover your handheld:
Important: These steps will wipe (delete) all your data and settings from your device.
- Remove all cables from your device.
- Switch your device off by holding the Standby button and swiping to the right.
- Hold the home button and connect your handheld to a running iTunes. Keep holding the home button until the "Connect to iTunes" logo appears on the device.
- In iTunes, click "Restore".
- In iTunes, click "Restore and Update".
- If you are shown an iOS Update screen, click "Next".
- On the License Agreement, click "Agree".
- iTunes now downloads the latest iOS software and puts your device in "Recovery Mode".
- The update and factory reset can take up to an hour.
Update: This does not work for iPhone 7 (and later models) as the home button functionality has changed. On the iPhone 7 you have to press the Standby and Volume Down buttons at the same time.
Update 2: On newer devices with no Home button, the procedure has changed. Please see this blog post.