How is rooting done under the hood?
Basically, it involves exploiting some vulnerabilities(for example, a buffer overflow) in Linux kernel to gain advanced privileges. The exact technique used varies with devices and Android versions. This Stackoverflow answer should give some idea. Androidcentral has a page that gives more information on root.