These One-Click root apps, like ol' faithful kingroot, follow a general structure for rooting. I will be using kingroot as an example.
Phase one- Detection of device - Self explanatory, really. Kingroot scans the build.prop (which it can, its mostly what you see in about phone) to grab and nab what the hell you are using. It can use other info sources too, but I don't know which (kernel, maybe)
Phase two - Exploiting - one-click apps generally follow a pattern of exploiting weaknesses in the OS to access /system directly (and freely). Malware and other infected apps/files can do this too, to gain their own root privilege. Anyway, they use this to gain temporary root.
Phase three - Planting files - at this point, The app just injects the SU binaries and SuperUser Management apps (in the case of kingroot, itself). It often makes these apps system apps to help protect against root access loss. Anyway, the temporary root planted previously is used as leverage, which allows for the root-app to provide permanent, system-wide root
Final phase - polishing - assuming that everything went well, all that's left is a reboot (to load the new binaries in. You may not even need a reboot, but you should anyway) and you are done!
su
and finally installing a Root Access Management app likeKing User
as a system app.. Some of these so-called 'automatically root' apps are open sourced, just check them out. – Gokul NC Jul 16 '16 at 15:31