Yes, jailbreaking is legal. Well, I'm no lawyer and every country has its own laws, so don't take this as a legal advice. Maybe you are not even allowed to have any
iPhone in your country.
But: For example in the United States of America, which has very strict laws in that context, like DMCA, jailbreaking and unlocking cellphones is now officially legal since a court order from 26 July 2010. Here's a link to the press release: http://www.eff.org/press/archives/2010/07/26
In many other countries there might have not been any official judgement yet, but the situation is similar.
Why should a manufacturer have the right to decide on what you are allowed to install on your device? Imagine you buy a new PC with Microsoft Windows on it. Then you find out that you cannot install Firefox on it, because Microsoft doesn't allow it. And you cannot even install Linux or anything else either. That's about the same. Or if you buy a house and the manufacturer still can decide what pictures you are allowed to put on your walls. It's your house, you bought it! This is about jailbreaking.
Maybe it's just because of the name or because some people use it for illegal activities. But there's no reason to be afraid of jailbreaking. Just read about it until you know what you do before you start.