What does jailbreaking an Iphone do, and how is it done?

Also is it legal?

Yes jailbreaking is legal, however it IS against Apple's terms of service meaning if you're jailbroken and something goes wrong with your phone, Apple can refuse to fix it as the terms of service has been voided.

Jailbreak essentially allows you to install 3rd party software to your iPhone, this will allow you to install apps that are not on the App Store, custom fonts, themes, tweaks to alter the look and performance of your device. Essentially it makes your iPhone very customisable.

Although jailbreaking is legal, what you can do with it can be illegal, for example you can pirate apps, movies and music. Doing illegal things on the device increases your risk of getting malware or damaging your device so it's best to stay legal. As long as you stick with trusted sources (repos) and don't start downloading illegal apps then you should have no legal problems running a jailbreak.

Note: As Apple don't like people jailbreaking, they're constantly trying to patch the exploits used to jailbreak, so if you want to have the latest iOS updates such as the new iOS 9.1 then don't bother jailbreaking as you will constantly lose your jailbreak, then have to wait for one to be released for that version, then have Apple release another update and you start all over again. To put things into perspective, the latest iOS is 9.1, I'm still running 8.1.2 as i'm waiting for a 9.1+ jailbreak

Jailbreaking is basically hacking the phone
Done through software
Yes it is legal though it voids the Apple warranty

Its a great way to get viruses and keyloggers.