In ancient or medieval battles, when two armies committed their first line of massed infantry on each other; what happened at the far end edges of both battle lines in regards to flanking?
You cannot flank the enemy maniple/unit in the center of a battle-line but you can do it at either of the edges.
When the two opposing enemy lines collide, the front-rank men will engage each other but whats stopping men [at the both edges of battle lines] from the back of either line to break formation and move around the flank and hit the enemy line in the back?
Interesting question! Flanking maneuvers certainly happened with infantry and they could be decisive. Such a maneuver was decisive at Cynoscephalae, for instance. However, as infantry generally fought in formations for tactical, command, and psychological reasons (men naturally stay together when afraid, the more the fear often the more they physically converge, such as Caesar's right flank at the Sabis, or the Romans at Cannae), individuals did not leave their formations to make attacks on the enemy in small groups. This would compromise the integrity, physical or psychological, of the formation to which they belong in the line, which might have a negative practical impact, aside from the general psychological reluctance of soldiers in ancient battle to leave the physical safety of proximity to their fellows.
Even assuming that soldiers/warriors would be willing or were allowed to leave their formations/groups to attempt to lap around or flank the enemy, their opponents were not defenseless against lone or small groups of attackers: an individual soldier or small group of soldiers would be little threat to a formation of combatants, as most combatants being 'unengaged' at any given time, would be perfectly capable of individually fighting back against very small numbers of flanking infantry.
Finally, the flanks of ancient armies were often protected by cavalry and light troops. Fighters who left the safety of their formation to attempt to 'lap around' or flank the enemy may expose themselves to cavalry units, who tended to ride around the edges of enemy infantry formations, remain in tactical reserve, or face off against enemy cavalry. They may also expose themselves to light troops, prominently the missiles that light troops tended to carry.
I don't doubt that to some extent, infantry would attempt to 'get around' each other to defeat their enemy where possible. It is important to note that in many accounts of ancient battles, care was specifically taken by commanders to make sure their army is of similar frontage to that of their enemy (eg. Marathon, the Allia), even if that means making their formations thinner, and the use of terrain such as troops posted on higher ground to secure the extreme flanks of infantry lines was common wherever possible (eg. Livy's account of the Allia). So as you say, men may attempt to 'lap around' the enemy, but they must have often been prevented by physical or psychological factors from doing so. Actual flanking maneuvers, which would take longer (as they have to go further, perhaps even using 'dead ground' to conceal their movement: the battle could be decided while such troops were gone) and require more troops and organisation, required control and leadership that would be generally beyond the capacity of individual soldiers to organise in battle. For any number of reasons (tactical or psychological), officers appear to have utilised such maneuvers somewhat rarely, though when circumstances allowed they certainly occured and could spell the difference between victory and defeat.*
*Aside from Cynoscephalae, the Battle of the Sabis in Gaul, 57 BC, is a prominent example of a 'junior' officer (actually a senior legate) organising such a battle-winning maneuver on his own initiative.
Is it perhaps alright to interpret it this way in absence of solid explanation by any ancient/medieval historian that: At the center, battle lines were thick and were kept in formation. At the edges, the battle-lines started thick but there was a constant leakage of men who kept wanted to "flank locally" on the edges of the battle lines.
So in a battle of "=" vs "+", the battle would start out as:
==============================================
==============================================
==============================================
++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++
but after some time the situation would be like this:
==========================================
============================================
====================================================
++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++
This roughly denotes that there would be constant leakage at the edges but to a certain extant. And as you move a birds-eye view away from the center and arrive at either edges, you will see that the thickness of battle-lines gets thinner and thinner up-to a certain point.