Understanding our battery
In summary: the gauges of the laptop power settings with the actual condition of your battery do not allow always to the the system to properly shut down.
- What is happening?
The system has not enough time to properly shut down all the services, save the files and dump the memory before the power level is too low.
Alternatively, if the system is set to go in sleep mode and not in hibernation, the
battery discharges without that the systems wakes up and dump itself in hibernation.
- What's the cause?
The battery discharge rate is not linear and constant in time, and it is not exactly the same for each cycle; it depends from the energy requests, the age of the battery, the external temperature, and the characteristics of the specific battery. If the battery is old, damaged, or used in challenging external condition it can abruptly be discharged. (See the further reading below).
Why only sometimes and not always?
In a working laptop, the discharge depends from the requests. The requests of the system/programs in that critical moment can be so different to allow to the system sometimes to save all and sometimes not. Even the quantity of energy communicated by the battery can be different from the real one and the system can take time that it really doesn't have.
What else can it be?
It can be the CPU/GPU temperature protection, if the temperature grows too much. There can be a spike of requests when the computer want to hibernate that increases the CPU temperature, the accesses to the disk. It can be a contributory cause for the abrupt discharge too, many requests more energy used, all at once.
How to patch this situation?
If the problem is that the hardware and software data are not matching, you should try to fix for first the hardware side. If it is not needed or possible, you can try to change in the software the data interpretation (patching the lecture of the discharge level in your system). In this optic you can even set different levels after which the system starts his hibernation:
- You can recalibrate the battery gauge [4b], there are different methods and tools, search for one suitable for your laptop. Many vendors provide tools and procedure, e.g. for HP.[4HP]
You can change your settings, creating a power plan specific for the condition of your battery ; giving a look below to the Further readings section, you can have hints about how much you should to increase the minimum level, of you can proceed with trials and errors:
Control Panel - Power Options - Power Plans - Change plan Settings -
check the Advanced Settings for drain and usage settings [4c]
Give it a look to the other answers ,[3b] too.
Buy a new battery: sadly for an hardware irreparable failure, or simply age, you will be forced to.
Simply speaking the battery is like a bottle of water, how fast it will be empty depends on its capacity, on how much water you take per minute, on the quantity of water the bottle leaks even without requests. In this analogy you can see how the water exit more faster from the bottle when it is almost empty then when it is almost full.
The discharge of a battery depends on many factors and differs from type to type, but, for what it is important here, presents a behaviour similar to the one of the picture below; note that when it starts to fall it falls fast:
Image from a blog . In red the line below the one the battery can say (the system can assume) that it is fully discharged (and stops to work). The age, or the defects, can deviate the behaviour of the battery from the expected one; in particular it can be more abrupt the sliding on the right part of the graph.
The voltage of the battery is related with the residual capacity and it is communicated to (read by) the system.
Image for another battery that shows the correlation between the Capacity on the horizontal*axis, and the voltage on the vertical one. Different colours for different temperatures. Note that I didn't expect that Windows or the battery are taking in account the different curve function of the temperature. So I expect that will work on a single curve, an average one, (so a not completely right one).
Now if this is not accurate or if the systems for spikes in the requests needs more energy, it will not be able to properly shut down.
At the end of all this reading take a moment to smile on your problem because the most simple answers were:
- "It is shutting down because it isn't plugged in" Ramhound 
- "You lost the files because you didn't save them regularly" Hastur and J. de La Palice