Describe write-ahead logging (WAL) and how it ensures data durability and consistency during an unexpected system crash.