They are unfortunately almost always shown incorrectly on TV and films, the patient flat lines and the defibrillator is used to restart it.
This isn't how they work!.
The name gives it away really, 'defibrillator' - it's used to treat a heart that's in 'fibrillation' - beating uncontrollably, more a fast 'flutter' than a beat.
The heartbeat should be controlled by the brain, rather like a timer interrupt routine in the brain
However, if the heart doesn't receive signals to beat, it can generate it's own 'signals', this is what fibrillation is - the heart controlling itself badly, rather than the brain in charge. So the defibrillator is used to stop the heart, and allow the brain to take control again.
(Crude explanation, but that's the basic idea).
Modern portable units are extremely easy to use, you open the lid and it's got instructions printed there, and if you can't read?, it will even give you verbal instructions. Basically you place the electrodes where it tells you, then press the button - the unit checks that the heart is in fibrillation, and only then gives the shock - so it's really pretty fool proof (but expensive).
Obviously they are stuffed full of electronics, I wonder if they use PIC's?.