A redirection connects a standard stream of a command to a file: > f sends standard output to f, emptied first; >> f appends to it; < f reads standard input from it; 2> f sends standard error; and 2>&1 sends standard error wherever standard output goes at that point of the line. Redirections are performed by the shell before the program starts, from left to right.
ls. With > all.txt alone, the error still reaches the terminal; 2>&1, written after it, sends the error where the output goes now, to the file. Written before > all.txt, it would copy the old destination, the terminal (Exercise 8.6).