Why is the heart not in the middle of the body?
First of all, let me make it clear that the heart is at the vertical centre of the body -- it is not shifted towards left (or right). However, it is slightly tilted towards the left in most cases. 



In some cases, it is tilted towards the right, and the condition is called Dextrocardia. For why it is so, lets look at what the heart does. Below is a diagram of double circulation (from here).



As you see, the highest pressure needs to be generated for pumping oxygenated blood into the body. Thus, the left ventricle needs the thickest muscles for this purpose. And due to these extra muscles, the  heart appears extended and seems shifted towards left.

Coming to the evolutionary perspective, it is important to mention that humans are not the only organisms with this feature. Indeed, displacement of the heart towards the left is a conserved feature in all vertebrates (Fishman et al, 1997). See this answer for more information.

Coming to genes, bending of the heart towards one side is actually controlled by the NODAL gene during development. See this diagram (from Jensen et al, 2013):



Tilting occurs in two phases, one during the first four and a half months of intrauterine life and the other, which is actually a 45° rotation to the median plane, later. During the early development of the heart, a process called cardiac looping happens and the straight heart tube develops a bend (see diagram). The NODAL gene, along with the Lefty1 and Lefty2 genes, regulates the speed and direction of cardiomyocyte movement during the development of the heart, leading to this asymmetry. To confirm it, researchers knocked out the spaw/nodal gene from a zebrafish and found randomized development of heart, even symmetric heart, as the result(!) (see Walmsley, 1958 and Rohr et al, 2008).

Now, talking about why this happened in the first place, and why it is so conserved among vertebrates, we need to ask ourselves a basic question: what good would a symmetrical heart be? External symmetry is preferred (probably) because it helps in locomotion; it would be quite difficult to move with your two legs placed away from your center of gravity. But when we talk about internal symmetry, conditions drastically change. We get a major restrictive factor here: space. And limited space always dominates other factors. Seeing that the structure of the heart is necessarily pointed towards one side, it becomes difficult to make it symmetrical. (The only option IMO is to have another pointed end at the right side.) In this case again, what advantage would a symmetrical heart provide? None. And it might even be harmful since having an even bigger heart would mean making both lungs smaller. Thus, a symmetrical heart would only prove to be a liability rather than an asset. See this question for more information.