You need to have some backup power to run your computer for atleast 1 minute after the power goes out.
If you have a UPS or any sort of backup power, You can direct your System to shut down or hibernate or standby when it goes to battery mode.
Considering you have a Battery Backup like a UPS, here is how you can shut down the system automatically when it goes to batter mode.
1. Right Click on the Desktop and select properties ( Display Applet from Control Panel.)
2. Switch to ScreenSaver tab.
3. Click on Power.
4. Switch to Alarm tab.
5. You have two option, one for the Low Battery alarm and one for the Critical battery alarm.
6. Choose the power level at which you want the system to shut down or do as you like , and click on its Alarm action.
7. In alarm action, you can select wether you want the system to give you an Audio Warning on Low Battery, A text warning.
8. In the Alarm Action section, Check the When the Alarm goes off action, and select wether you want the computer to standby or shutdown.
9. Click OK, Again an OK for the Power Window and Again an OK for the Properties Window.
Thats it, you are down. Keep in mind that unless a computer has a battery backup, it cant be On even for a Second after the power goes.
Hope this helps you.