Programs like login, sshd, or su work by initially starting as root, either because of their ancestry or by having the suid bit on the executable file, and carefully restricting what you can do until you authenticate with a password or other method, then changing to an appropriate uid (either root or the user you are logging in as) and exec'ing another program (usually, the shell).