Mass formations had been in use since ancient times. The main reason for their use was to move large units of men, companies, regiments, brigades etc. around on the field in order. Those tactics evolved to what we have now, wars being fought with small squads and platoons, and firefights taking place here the there, do to rapid fire weapons and smaller unit tactics being produced. Back then, in the 1800s, those line and rank formation tactics were all they had, there was no other way to fight conventional warfare, other than digging in and fighting behind earthworks, which did happen very often.
Back then, battles were fought in a day or two, with huge armies fighting out a battle in a space of a few miles usually. You have to think of regiments and brigades being moved around like chess pieces, whole units of men had to be moved around on the battlefield and be able to engage the enemy, and formations were the way to do it.
Every battle was different and there was a lot more to them than "two sides lining up and shooting back and forth". I like to think of it this way: there are battles of attrition, where two sides fight over a certain amount of ground and both sides push each other back and forth, like the fight in the cornfield at Antietam. Then there are the battles where one side is on the defense, which usually means digging into earthworks and sitting in one spot, defending that ground and holding off the enemy's attacks, like Fredericksburg or Franklin.