A topological sort takes a directed acyclic graph and arranges its nodes in a line so that every edge points forward in the line. If there is an edge from A to B, then A appears somewhere before B. The intuition is dependency ordering: if task A must be done before task B, A comes first. This is the exact problem a build system solves when it decides what order to compile files in, what a package manager does when it installs dependencies before the things that need them, and what a scheduler does when it sequences jobs that must precede one another.
A topological order exists only if the graph has no cycles. A cycle is a circular dependency, where A must come before B which must come before A, and no linear order can satisfy that. So topological sorting and cycle detection are two sides of the same coin: an attempt to sort that gets stuck has proven the graph contains a cycle.
There are two classic methods. One, often credited to A. B. Kahn in 1962, repeatedly removes a node with no remaining incoming edges, appends it to the output, and deletes its outgoing edges, exposing new edge-free nodes until the graph is empty. The other is built on depth-first search: run DFS, and list the nodes in reverse order of when they finish. The MIT 6.006 lecture on depth-first search presents this second approach directly, deriving a topological order from the finishing order of a depth-first traversal and using the same traversal to detect cycles.
Either way, topological sorting runs in time proportional to the number of nodes plus the number of edges, the same linear bound as the underlying traversals. The result is rarely unique: when several nodes have no outstanding prerequisites, any of them may legitimately come next, so a graph can have many valid topological orders. What matters is only that the chosen order never places a task before one it depends on.