Jailbreaking and rooting both refer to resetting the operating system on your phone to a more basic level so that you are not limited in your use.
Jailbreaking was a term first used with iPhones to break the limits Apple put on the phone allowing you to only use the apps from their store. The idea of Apple is that you are using only apps they have approved and know are safe. However people would like to put other apps on their phones that they take risks with. The term jailbreak came from that people were jailbreaking from the Apple Apps store.
Rooting is wiping out other phones and reinstalling the android operation system without the limitations on the operating system that the manufacturer or your carrier has placed there. Partly that was put there to try to limit people to safe apps. The term rooting comes from that the Android operating system is actually a variety of LINUX/UNIX and the most basic level in it (or user with all authority) is root.
Rooting will allow you to easily use a phone with a different carrier which was a bigger thing in past as it used to be you had to get phone from carrier. Now in many cases the phone service carriers will allow you to bring a device from another carrier.
There is a risk in jailbreaking or rooting a phone and that is that you my make it where it no longer operates. If you do want to do things on the phone that the manufacturer or carrier blocks then jailbreaking is an alternative to consider.
The US government has ruled in recent years that jailbreaking and rooting are basically legal, but there continue to be legal questions on the legality. The manufacturers continue to try to make illegal and in many countries it is considered illegal.