This image is in the public domain.
There is a difference between "jailbreaking" and "unlocking" your iPhone.
The terms have been used interchangeably, even in the Press. This is incorrect. Jailbreaking and unlocking are quite different things.
The difference between jailbreaking and unlocking
- Jailbreaking your phone lets you install applications that you don't get through the Apple App Store. This would include applications that Apple hasn't approved yet, or has rejected for competitive reasons, or has rejected out of agreement with carriers (e.g., an app to allow you to use your iPhone as a 3G modem for your computer).
- Unlocking your iPhone allows American owners to use the phone on GSM networks other than AT&T (e.g., T-Mobile, Sprint).
The legality of jailbreaking and unlocking
Are either of these activities legal? Originally, Apple wanted you to think not. They made sure everyone was aware that both were violations of the service agreement and "might be" illegal. The US judicial system rightly concluded that a service agreement does not carry the same weight as a business contract, and that consumers who own a tool are allowed to use that tool in any legal manner they see fit to.
Apple's response was a nonchalant, "Yeah. We figured that. But it's still a violation of the service agreement."
In other words, if Apple finds out you did either activity, your warranty is voided and you won't be able to make claims on your phone for repair or replacement.
How would Apple find out? Well, they probably won't. It serves no business purpose whatsoever for them to monitor data in order to cancel service agreements. And, in theory, if you jailbreak your phone, you can "unjailbreak" it in a way that leaves no traces. Presumably, since unlocking is accomplished through a third party application on a jailbroken phone, you can un-unlock it, too.
Given those caveats, although you may forfeit your warranty, you are breaking no laws if you jailbreak or unlock your iPhone.