I know Cyrus died at Cunaxa, and Harold at Hastings; but Alexander rode into battle again and again, and Hannibal was front and center at Cannae. Given how much these great men seem to have thrust themselves into the awful din of battle, and how attractive a target they must have made to their foes, they seem to have survived a great deal more than one might imagine they would.
Is it merely a selection bias that causes us to remember the generals that survived long enough to cover themselves in glory? Is the middle of a battlefield safer than it seems? Or is there a lot to be said for a good bodyguard?
I don't think you're premise is necessarily correct. I can't speak of the Middle Ages, but certainly in antiquity this is not the case. Frequently armies only routed once their leaders were killed--we see this over and over in the wars of the 5th and 4th Centuries. Just a random selection of battlefield deaths of prominent generals would include:
The polemarch Callimachus at Marathon
Hippocrates at Delium
Both Nicias and Demosthenes during the retreat from Syracuse (ok, this doesn't really count, as both were captured during a prolonged campaign of retreat and executed, but both--particularly Demosthenes--led their troops conspicuously from the front and were captured among their men)
Both Cleon and Brasidas at Amphipolis--Brasidas was one of only 7 Lacedaemonian dead, despite winning the battle
Epaminondas at Mantinea
Antigonus Monophthalmus at Ipsus (leading his Companions at the ripe old age of 81--dude was a badass)
Flaminius at Trasimene
Paullus at Cannae
I'm going to stop there, because the list would be exhaustive (and if I really wanted to do it thoroughly would take hours). But this I hope gives an idea of the incidence of casualties among generals--and this is hardly complete, only whatever popped up into my head for an approximately 200-year period. I also avoided battles in which the generals had been wounded seriously--those would be far too numerous to count--and sieges, in which casualty figures would be rather irregular. But as you can see, defeated armies frequently encountered the loss of a major leader, at which point cohesion and morale would rapidly break down, and even victorious armies would sometimes suffer such a loss, particularly in the later stages of battle. It's been suggested that armies in the ancient world didn't really rout until a general had been killed, but this is really untrue and those (like Hanson) who suggest it clearly have not gone over the accounts of battle thoroughly enough--more often generals were killed, in defeat or not, during a rout, not before. Still, death of a leader in combat was nevertheless a major component in loss of morale and discipline, so it can't be discounted. Casualties among officers in general seem to have been extremely common among officers in armies of the ancient world. We have rough estimates of the survival rates among centurions and the casualty figures given by sources often list how many centurions and other officers were killed. In both cases the mortality rates appear to have been extremely high. In Greek armies we find constant references to the extreme danger of the front few ranks, which were considered extremely hazardous, and it seems that in many cities the file-leaders were fairly important officers, although we don't have much literature on Greek army organization before a certain point and it's not very helpful. But it's definitely not the case that battlefield deaths among generals and officers were uncommon
Well, it is important to remember that pre-modern sources were often sponsored in some way by the people whom the source writes. So, my thinking is that while the sources may say that the leader was a valiant warrior and actually put himself in danger, it is very possible that the source is lying and that the leader never actually put himself in danger.
For obvious reasons, this is difficult to prove, but it is something to keep in mind when reading pre-modern sources.
Also, it's not without precedent for a leader (king, lord) to die in battle. King John II of France was captured during the Hundred Years War at Poiters, several significant nobles were killed at Agincourt, and looking back farther, triumvir Crassus was killed in a skirmish with the Parthians.
Generals were protected by some of the best soldiers around who could typically afford the best equipment of the day. Compare this to the average soldier of a non-professional army who would likely be a poorly equipped peasant with little or no military experience. I can't say how often generals 'got into the fray' with the enemy but in the event that they did they were often surrounded by a group of men who's sole duty was to protect them. Adding to this, generals typically had combat training and equipment that the vast majority of people didn't have.
XenophonTheAthenian gives a tremendous response regarding the Greek and Roman periods but during the middle ages this does seem to have tapered off somewhat. What you do start seeing more and more, particularly in the high middle-ages are people being taken captive and this has a lot to do with two things.
Firstly, the advancement of armor. Only the elite of the elite, the generals and their high ranking officers, would be in full plate mail armor and wearing this gives someone tremendous survivability on a medieval battlefield. It takes real effort to kill someone wearing a few millimeters of hardened steel over all their vitals. It was not unheard of for someone to be knocked down or unhorsed and in the push and confusion of battle be unable to stand and fight again while being completely protected and unharmed inside their armor whereas previously this had never been the case. This made Generals far less likely to die from someone just getting lucky or some random arrow.
In addition to that the middle ages saw the rise of the practice of ransoming prisoners. If a general or king was found alive inside their armor on a European battlefield there's almost no chance they would be killed. Instead they would be taken prisoner and negotiations for their ransom would be started. Where previously a soldier who killed an enemy general could expect to be covered in glory and well rewarded a soldier in the high middle ages would be far more interested in capturing an enemy general alive so that he could then be ransomed. Richard the Lionheart is a famous example of a General/King who was captured and ransomed whereas a few hundred years before he would likely have been killed/executed.
Edit: This book goes into the culture and development of ransom through the high-late medieval period and discusses many of the aspects of its early development as well. The review and discussion of it is also fairly interesting http://www.history.ac.uk/reviews/review/1465