What you want is called a UPS. There are many threads on here about them so a search should get you pretty much all the info you need. Personally I go with the APC brand other than replacing the batteries when they are old and dying I have never had a problem with their products.
I am a +7 for APC. Been using mine for years, other than replacing the battery a year or so ago it has worked perfectly. It also integrates into OS X extremely well, DO NOT install any software to manage it, that is built right into OS X already.
I have 2 APC UPS devices in the house, 1 for my iMac and one for my wife's MacMini. They supply a special USB cable that causes OSx to recognize that there is a UPS and software deals with it. Don't install the APC software - just let OSx handle it.
I have a couple of rather old six-outlet Back-UPS ES 350 units for things that don't draw a lot of power (like my DSL router and AirPort Extreme).
I have a slightly better eight-outlet Back-UPS ES 550; this one has "green features" which has a master socket that will control the power of a couple other sockets. Thus, when I turn off the computer monitor, it will cut off power to two other devices (in my case a stereo amplifier and a powered subwoofer).
As the others say, there's a USB cable that enables the Mac's UPS management features; do not install the third-party software. I have my Mac mini set to shut down when the UPS battery level drops to 5%. It has never had to do so, as power failures in my neighborhood are infrequent and rarely go longer than an hour.
APC makes fine products, I have been a happy customer for 15+ years.