Before you factory unlock your iPhone check the unlock availability:
Step 1 - Select your iPhone model
Step 2 - Select the network the handset is currently locked to
Step 3 - Enter your iPhone´s IMEI (15 digits long)
Step 4 - Click "Unlock Now" button
Make sure you are using the latest version of iTunes.
Step 1 - Insert a NON AT&T sim (it can be any sim card from any other carrier such as T-Mobile etc...)
Step 2 - Plug the iPhone to iTunes and do a Backup and Restore (backup, restore are normally optional and may not be needed)
Step 3 - If your iTunes freezes you need to unplug the iPhone, wait 10 seconds and plug the iPhone back to iTunes and do the restore again.
Congratulations, now you iPhone is permanently unlocked.
Enjoy your new more fun and functional iPhone!