It depends on the reason the person died and if the person died of brain death then the heart can stay alive and working a year or more.
If the person dies of other things then the heart will usually stop beating within minutes or the same day if the person is not on life support.
If the person is on life support the heart can continue beating until you remove the person from life support.
In the case of a heart attack the heart could stop completely within a few minutes and if not restarted it can result in death within a few minutes afterwards.
Some peoples hearts are still okay and beating after brain death and can be transplanted to someone else needing a heart donation.
The cessation of cardiac activity is how death is usually defined and the cardiac muscle will be alive for about 10 minutes after that happens.