A race condition is a defect whose outcome depends on the order in which two or more concurrent operations happen. TOCTOU — Time Of Check to Time Of Use — is the security-relevant form: between the moment a program checks a condition and the moment it acts on it, an attacker changes the underlying state. The check is satisfied for one entity (the original file, the original owner); the use happens against another.
The classic example is filesystem races against setuid binaries. A root-owned program checks permissions with access() and then opens the file with open(). Between those two calls, an attacker swaps the user's file for a symlink to /etc/shadow. The privileged process opens what is now a link to a sensitive file and faithfully prints it. The bug isn't in either system call — it's in the assumption that the filesystem doesn't change between them.
Races appear anywhere two paths touch shared state: kernel concurrency, web requests against the same database row, lock-free data structures, signal handlers, and even hardware speculation (Meltdown and Spectre exploit micro-architectural races). The defensive principle is the same in every case: operate on a handle, not a name. Open the file once, then ask the kernel about the file descriptor (fstat, fchown, fchmod) — nothing in user-space can substitute a different file behind your back once you hold the fd.
When you can't operate on a handle, the answer is an atomic operation: a single primitive that performs check-and-act indivisibly. Compare-and-swap, file locking, database transactions with SELECT ... FOR UPDATE, or O_CREAT | O_EXCL when creating files. The shape of the fix changes; the principle — collapse the window to zero — does not.