In a nutshell
To survive crashes, a database keeps a write-ahead log: it writes down each change before applying it. When it restarts after a crash, it scans the log from the last checkpoint, redoes the work of transactions that had committed (using the recorded new values) and undoes transactions that hadn't (using the old values), restoring a clean, consistent state.