There are many different ways to organize files and directories in a computer, but in the abstract of how that happens at the hardware and OS level is typically assigned to the file system. This is how it works.
https://www.techspot.com/article/2377-file-system-explainer/