Heart rate is regulated by homeostasis, a process by which the body regulates its internal environment during its interaction with the outside environment. A prime example of another variable regulated by homeostasis is body temperature. When the environment is too cold, the brain triggers muscle contractions to raise internal temperature.
Homeostasis in an involuntary process and, although many factors contribute to the process, it's mostly controlled by a region of the brain called the medulla. The medulla plays a key role in regulating heart rate. When the body requires more oxygen, the medulla releases certain neurotransmitters that control electrical activity in the heart, allowing heart rate to increase or decrease depending on the body's needs.