To expand on this a bit, defibs will only deliver a shock if they detect VF (ventricular fibrillation) or VT (ventricular tachycardia). These are considered shockable rhythms. Both involve malfunction of the electrical conduction system usually due to lacking oxygenated blood supply to the heart muscle itself.
A shock seeks (usually up to 300 joules depending on patient size) to reset the hearts' rhythm so the body's natural pacemaker (called the SA node, positioned in the top-right of the heart) can kick back in. If there's no shockable rhythm, the defib machines will usually instruct you to continue CPR (chest compressions) and then will ask you to stop after a cycle of 30 so it can re-analyze the rhythm. Otherwise CPR must continue until medics arrive. If the defib detect a normal rhythm (sinus rhythm) then that's good news and means the defib has been effective.
Pad placement is very important if you're administering defib. The pads are positioned so that the depolarization travels approximately down the septum of the heart, to emulate the normal electrical cascade.
Note: there are many types of heart malfunctions that can't be remedied by a defib but that doesn't mean you should do nothing. Always do CPR. Always call the emergency services.
Possibly of interest to HNers: It is a common misconception that a defib can bring a heart back from 'flat-lining' (asystole). Hollywood movies don't help. If there is asystole, medics will usually continue CPR until the rhythm changes whild trying to reverse any underlying causes of the heart failure (e.g. massive hemorrhaging or hypothermia).