The program is a small pipeline
The GNU ls program does four jobs in order. First it parses its options and its arguments. Then it opens each directory it must list. For every entry it reads, it may ask for the metadata. Finally it sorts the entries and writes them in the chosen format. Each job depends on the kernel for something different. The opening and reading depend on the directory operations of the filesystem. The metadata depends on the inode. The formatting depends only on the program, and it is the part most people look at.
The program is not a special case. Other directory tools, such as find, follow the same basic steps. What differs is how much of the metadata each one needs, and that difference is the main source of the performance gap between listing names and listing full details.
Opening a directory
The C library call opendir opens the directory with the openat system call and returns a handle. The kernel checks the read permission on the directory and returns a file descriptor. Notice that the check is on the directory itself. A directory with read permission but without execute permission lets you read the names but not follow them, which is the source of a subtle rule that the permissions module covers in detail.
Reading the entries
The library call readdir is built on the system call getdents64. The kernel fills a buffer with records, and each record holds an inode number, an offset, a type byte, and the name. The type byte is a hint that comes from the directory entry itself, which lets a program learn the type without a separate call on most filesystems. The names come back in the order the filesystem stores them, which is rarely alphabetical and is not stable across runs. That is why sorting must happen in the program.
A single call returns as many records as fit in the buffer, so a large directory takes several calls. The program repeats the call until the kernel returns zero bytes, which signals the end of the directory.
Asking for metadata
If the chosen format needs more than the name, the program calls lstat or the related fstatat for each entry. The "l" means that the call does not follow a symbolic link, so the program sees the link itself. For a long listing, this means one system call and one inode lookup per file. For a directory of ten thousand entries, that is ten thousand calls, and each one may read an inode from disk if it is not cached. This is the reason ls -l can feel slow on network filesystems and on very large directories.
The flag -f in GNU ls turns off sorting, implies -a, and skips the metadata calls when no long format is requested. The result is much faster for huge directories, and it is the right choice when you only need the names and their order does not matter.
Sorting and formatting
After the entries are collected, the program sorts them. The default comparison uses the collation rules of the current locale, which is why ls can produce a different order in different environments. Then it measures the widths of the columns, chooses how many columns fit the terminal width, and writes the output. When standard output is a terminal, colors are added by looking up file types and modes in a table that the environment can change.
Setting the variable LS_COLORS changes the colors that ls uses. Setting TERM or redirecting output changes whether colors are used at all. None of these settings change the files on disk.
What the kernel does not know
The directory read does not give the program a path. Names are relative to the directory handle, and the program combines them with the directory name to build a path for stat calls. A name that contains a slash cannot occur, because the kernel forbids it inside a directory record. A name can contain a newline, however, and that is the reason line-oriented parsers of ls output are unsafe.
Lessons for your own tools
If you write a script that needs the names in a directory, a glob is the simplest and safest choice, because the shell does the directory read and the sorting for you and passes the names as separate arguments. If you need metadata for each entry, find with an action such as -printf or a single stat call over many names is far more efficient than a loop that runs one command per file. The principle is the same one that governs ls: pay for metadata only when you use it.
You can watch this sequence with strace in the lab. Trace only the system calls you care about, and notice that the names arrive before the metadata calls begin.
strace -e trace=openat,getdents64 ls /workspace 2>&1 | tail -n 4