The cells in the heart don't "know" when something is wrong themselves. But those cells make up the muscle tissue which is the heart itself. That muscle tissue is connected with nerves that sense all sorts of information, like pain, pressure and chemical residues which are made when cells die. If something goes wrong with the heart's cells, and they are damaged or die, the nerves can sense that and report that to the brain. All our body's tissues have nerves in them and they can provide a great deal of information.
making a small donation to science.ca.