When a computer "crashes", it may, eg, be due to an "abrupt" power failure (in which case nothing can be done), it may be due to some sort of nasty internal event (eg, corrupted page tables) that similarly prevents doing anything, or it may be due to some condition that simply prevents "further operations" somehow -- perhaps new processes cannot be started, perhaps the display cannot be updated, perhaps the system has simply run out of storage.
In these latter situations the OS can still function, within limits, and can at least complete disk operations that were ongoing, nicely close files, etc. In addition, if slightly more function is possible, the OS may be able to tell the various application processes to shut themselves down cleanly.
But even if the system goes down "hard", the system as a whole and individual applications may have maintained a system of "journals" and/or "checkpoints" to allow the state of the system as a whole and individual applications to be restored to an "atomic" point, where everything is "internally consistent", and where a minimum of important data has been lost.
This all isn't accomplished with a single mechanism, but with layers of functionality at the system and application level.
With regard to power failures specifically, there may or may not be advance notification, and the "advance" warning may be a fraction of a second or (with UPS or a failing laptop battery) several minutes. What can be done depends on the amount of warning.
In most cases, with a desktop sysyem without UPS, there is at best time to "quiesce" disk operations so that no disk is in the middle of a write when power finally disappears. This prevents generating bad sectors on disk. It used to be, when RAM was much smaller, that there could be enough time (with large capacitors in the power supply, or even, oddly using the energy stored in the disk drive rotor to generate electricity) to write RAM to disk before power dropped, but that possibility pretty much vanished when RAM grew larger than 100M or so.
[And note that, in the olden days, when memories were made with magnetic "cores", RAM was inherently preserved when power was lost (though a particular word being read/written when power failed might be corrupted). This made it easier for those old systems to recover from power failure.]
However, with something resembling a UPS (which may give anywhere from a few minutes to several hours of additional power) there are several more options. One is to simply shut the system down, as if you requested "power off". This causes each application to end itself, and then the system as a whole writes out permanent tables and shuts down. This can take a long time (as I'm sure many of you have noticed). But it's also possible to use a "hibernate" strategy, where RAM is written to disk as a single block, and then the system powered off. In the "hibernate" scenario, on restoring power, the RAM is read back in to exactly where it was written from, a few bits here and there are swizzled, and then execution resumes where it left off.
On some older "big iron" systems a semi-hibernate strategy was used to do an emergency shutdown: Memory would be written out as above, but when power was restored and memory read in then a standard shutdown would occur. This was done because some bits of system state (particularly in I/O controllers) could not be reliably saved/restored to permit continued operation.