A computer doesn't actually "know" anything. It simply follows instructions, whether those instructions are good, bad, right, or wrong. It never searches for a solution on its own; it just produces the output of whatever it's told to do.
When you power it on, the BIOS chip activates using instructions hard-wired into it, identifying the motherboard and its components. The hardware runs diagnostic checks, and once they pass, the BIOS directs the hard drive to load the operating system, such as Windows or macOS. The processor then takes over, activating peripherals and running further diagnostics, storing the results in a system configuration file that other programs can reference.
That configuration file is what saves everyone effort. Think of it like a waitress and a cook: the cook already knows how to make a cheeseburger, so the waitress just calls out "cheeseburger" and it gets made. Without shared instructions, she'd have to explain every single step.
This is also why security works the way it does. A computer has no sense of loyalty or morality; passwords and encryption are just more instructions. If you type the wrong password, the computer isn't refusing you out of principle, it's cheerfully giving you the output your input produced: "Access Denied." And if someone enters the correct password, it will hand over the requested information without a second thought, because it can't tell the difference between the rightful owner and an intruder. It just matches input to instructions.
Put simply, computers are just vast collections of tiny switches flipping other switches in precisely the ways we've arranged. Every action they take comes from checking conditions in their code and reacting accordingly, looping through those instructions over and over.