Whenever I see line formations, I can't help but think how much smarter it would be to spread out. My reasoning is that it would be much easier to hit and kill someone in a line formation because they're grouped up. Spreading out would make it harder for the enemy to hit your army.
Here's something for you to consider: If they were ineffective, why would militaries continue using them? Wouldn't ineffective measures be taken advantage of by their opponents? What you're missing here is largely the pragmatics of musket use, which inform a significant portion of the tactics at play. I commend to your attention the appropriate section of the FAQ on Early Modern Warfare; you want everything under a heading that says 'linear tactics'. Beyond that, some further threads for your perusal:
One thing about the people of the past: never assume that they are not as smart as modern day people just because they don't have technology. A large portion of the time, seemingly silly decisions make a lot more sense in once analyzed through the eyes of someone at the time.
So what reasons are there to adopt the line for your infantry? First, you certainly wouldn't do it for all of your infantry. Skirmishers frequently fought in a way quite like how you described above. Loose formations and smaller numbers meant they were more maneuverable than their heavier counterparts, and were used to heckle enemy formations, to slow their advance or cover a retreat while chipping away at enemy morale. American Minutemen sometimes fell into the role of skirmishers, for example. Skirmishers are not the perfect base for an army, of course. They may be good at slowing enemy attacks, but if you tried to defend a position with skirmishers against determined line infantry or heavy cavalry it would go poorly. In a similar vein, skirmishers would be unlikely to dislodge well prepared line infantry.
So, why line infantry? One key thing to realize is that they were not working with nice modern guns, and so they tried to get more oomph out of their slow loading guns through volley fire. Having all of your men in one line fire at once while the other ranks reload means you're sending a lot of bullets at the enemy at once, the slow reload speed of the musket is made up for, and the poor accuracy of smoothbore muskets is countered by sheer volume of shot. Sending out loose skirmishing forces just would not be able to send the same amount of fire back at volley firing line infantry.
And of course, another reason why line infantry and big formations were important is cavalry. If you were in a loose, unorganized formation when enemy cavalry charged you, you were in for a very bad time. Even line infantry could be vulnerable to cavalry if charged from the side or the rear, and one way these charges were countered was by attempting to form a hollow square. That's not to say line infantry couldn't hold off cavalry (like the Thin Red Line at Balaclava, who held off a Russian charge) but it was the exception.
So why did line infantry fall out of style? Rifling was a key reason. Skirmishers like I mentioned above might have used rifles, but once rifles became widespread, the improved aim of the rifle meant that line infantry marching in ranks became easy pickings, like you thought. With higher rates of fire from breech loading rifles and eventually the machine gun, line infantry would take too many casualties before being able to close in.