How was it decided who would be at the front as opposed to the back? Would they ever "switch out" with other soldiers in the middle, or would they simply die and be replaced?
I imagine that this would be especially bad in Napoleonic or American Civil War era battles.
How did commanders convince soldiers to stand at the front?
This is going to be era specific but I can give you an ancient example that will give a bit of insight.
In the Pre-Marian Republican Army in ancient Rome the front ranks were the young Hastati. They were about 17-20, they were young enough to be in their prime and old enough to round out correctly as was needed for battle. The younger Velites served the role of skirmishers and would charge forward before the battle commenced to soften the enemy line.
The Hastati's sole place was the front of the army and much like later in Roman times, they would fight for a time and then alternate with the ranks behind them so that fresh men were constantly fighting.
Behind the Hastati were the Principes, usually older or veteran soldiers who could close the gaps if necessary and could act as battlefield expertise support and encouragement to the younger Hastati and behind them were the Triarii spearmen, the oldest and approaching retirement.
Therefore what you had were the freshest, most eager, and strongest soldiers to the front, consistently supported by soldiers in the back that could either encourage them (or if necessary tell them when to run), and the one's best suited for the front line battle.
I'm most familiar with 18th and 19th century warfare, so I'll discuss that. The "front line" of a unit could vary throughout a battle as a unit manuevered. For example, a unit going from marching column to line order, charging, reforming and returning to column would be shuffled up quite a bit.
As for casualty rates in the front ranks, soldiers in the front certainly faced a greater degree of risk than their comrades in the rear. At the Battle of Fontenoy in 1745 and the Battle of Quebec in 1759, the front line of some French units was wiped out by the first British volley.
However, safety was certainly relative. Soldiers in the rear were expected to quickly fill in any gaps that opened in the front ranks. And sergeants and officers would constantly be extorting (and pushing) soldiers to "close up." And artillery roundshot and canister could easily kill 3-4 men in a file. So being in the rear hardly guaranteed safety.
I'm not sure how you're defining ancient era, as you've included 19th century examples, so I'll give you an equally broad answer. Most armies across eras had reserves, and front line troops could be chosen for any number of reasons, including prestige, experience (or lack thereof) or just luck.
I recommend you look up John Keegan's The Face of Battle, which will give you an excellent in-depth view of front-line soldiers and fighting in the Medieval (Agincourt), Napoleonic (Waterloo) and Modern (Somme) eras.