the heart is myogenic - meaning it can carry an electrical impulse without the use of neurons. This signal causes contraction of the different cardiac chambers it passes through. The ECG, or electrocardiogram, measures and displays this electrical activity, which can then be used in a clinical setting to diagnose various arrythmias, such as atrial fibrillation.
In a healthy heart, the signal originates in the sino-atrial node, found in the walls of the right atrium. This means the first chambers to depolarise, and therefore contract, are both atria - causing the first peak seen on a typical ECG, the P wave. The signal then travels through the atrioventricular node, into the His-Purkinje network, and causes depolarisation of the ventricles from the apex upwards, represented by the QRS complex. Finally, the ventricles repolarise and relax, causing the T wave.