How does rooting work?
What kind of code/programs the manufacturers don't want to install in the phones (so that people have to root)? Why?
Can it be unrooted too?
Thanks in advance.
Question is very vague.
Root access is access to the root system of the device, usually the user is restricted to an environment that only allows reading of the system, and other data that is needed or added is added to the partition called Data.
Access to the root system allows modifications of system files, and sometimes root applications require root access to do this.
However Android is literally running on Linux, and linux has many ways to change your id to root, once you've obtained root permissions and a root id, you can modify system files to allow root access through a program like SuperSU.
Root access allows adding & removing or ( RW Read Write permission to the system.
Is it dangerous, well yes it can be...
For example.... Access to the system allows you to install system applications that can't be uninstalled, if a malicious application or hacker managed to install a virus, spyware or malware.. you would not be able to remove it without completely reinstalling the system.
Why do manufacturer's stop root access ?
It can be dangerous to require root access as i just mentioned, and it also allows users access to area's that they're not allowed to access like the Data partition...
Accessing each partition comes with separate risks and root access means access to everything.
People can pull song's and videos from applications that would otherwise be protected in the data partition for example.
Can it be unrooted too ?
Yes, however any modifications made to the system will be permanently applied... Factory reset will not reinstall the system like on Windows... Instead it just erases the Data partition to start from fresh.
This means once the root access has compromised your system integrity.. it is just that, compromised.
However you can reinstall the entire system firmware by using a custom recovery or program on a PC.