I know that heart attack is when some of cells are dead. That's why heart beats abnormally and needs AED to stop it and restart. After that it beats again normally and my question is - how come since cells are dead?
An AED stops the heart from beating entirely (beneficial in some situations), and has nothing to do with the death of cells during a heart attack.
The rhythm of the heart is regulated by electrical signals which are controlled by the sinoatrial node and the atrioventricular node. An adequate simulation of this can be seen in this gif:
Source for animation: Battlinbill07 on imgur.com
This cycle is fairly stable and beats throughout your whole life (although some beats are skipped and some people have heart arrhythmias from birth onwards).
Sometimes, the cycle messes up and heart arrhythmia ensues. Some types prevent proper blood circulation and result in the death of a patient. Some are also problematic because CPR is not efficient in those scenarios.
The AED sends a strong electric signal through the heart. For some shockable rhythms, this is recommended and will result in an asystole (a flatline). In some TV dramas, this causes the patient to wake up, but in fact a de-fibrillator only stops the heart activity entirely, thus also eliminating fibrillations. Sometimes, this will synchronise the nodes and the heart starts to beat again normally, and sometimes even the synchronisation doesn’t help and an asystole is the result. This is why defibrillating a flatline is totally useless, despite being often done in TV shows. After the AED created an asystole, CPR is most effective and will result in better survival chances than CPR with fibrillation.
A heart attack is the result of a blocked artery and leads to the slow death of the heart muscle due to low oxygen supply. The heart stops beating long before the entire muscle died, and some of the process is reversible. Usually, a heart attack leads to an asystole when the heart stops beating, which is not a shockable rhythm and an AED will not recommend a shock.
A cardiac arrest (wrongly used interchangeably for heart attack) is the general failure of the heart to beat and has many causes (including a heart attack), some of them arrhythmias where a shock may improve survival chances.