I know line formation makes sense when you're fighting with muskets, but since both sides had rifled muskets a line formation just seems to have made the soldiers easy targets to me. Was it to keep the soldiers organized during battle?
This is a more nuanced and complicated question than you might imagine, actually, but a good one nonetheless! There were a number of reasons armies on both sides of the ACW fought in a line formation, and while the common answer is that it was a reflection of outdated Napoleonic tactics, this is only partially true. Although some Napoleonic tactics (this is a broad term, and doesn't do justice to the many different styles of warfare Napoleon used, invented, and/or made famous) were still in fashion by the 1860s, they weren't seen as sacrosanct. Many developments and improvements had been made upon infantry formations, cavalry tactics, and artillery theory in the 40-ish years since Napoleon had waged his campaigns. In fact, as the ACW progressed, most commanders did everything they could to avoid presenting their infantry ranks to rifle and artillery fire when troops were arranged in a line formation.
For example, when on the defensive, although infantry might be laid out in something resembling a line, they rarely just stood there and took fire without any cover if they could avoid it. Union troops at Fredericksburg could attest to the futility of such a formation (arrayed in an exposed line formation) when approaching defenders with good defensive works (i.e., a sunken road, wall, or even foliage cover). Likewise, Confederates at Gettysburg, attacking the Union center on the 3rd day, would have complained about similar troubles. Wherever possible, commanders sent their troops along the flanks of the enemy, and when that wasn't possible, would use the line formation to swamp an enemy's position with raking sheets of fire that overwhelmed a numerically inferior, or otherwise frazzled enemy.
And this was the key: a well-disciplined line formation presented a horrible wall of lead that ripped through similar, exposed lines. In an open field, it really was a contest of numbers. All things being equal, the bigger, longer, and/or deeper line usually drove the other off if one side's artillery didn't come into play to tip the scales. This line formation was also much easier to command, both in terms of fire control and maneuver capabilities. A scattered group of people running towards a massed, disciplined opponent was (1) not very intimidating unless it vastly outnumbered the besieged, (2) wasn't easy to control if adjustments needed to be made in terms of the point of attack, (3) didn't present the same concentrated field of fire a massed enemy would, which had a very powerful effect.
So, to answer your question: lines of formation on the battlefield weren't always the norm, as armies often tried to swamp the flanks, and to do this, a rigid line formation wasn't always practical. When they were used, however, it was usually against a similarly exposed foe, who suffered the same disadvantages the attackers did. When line formations attacked an entrenched foe, it usually did so because their numbers overwhelmed the defenders, or at their peril (see Union at Fredericksburg, and Confederates at Gettysburg).
[Bruce Catton, 'Mr. Lincoln's Army'; James McPherson, 'Battle Cry of Freedom'; Jaw Winik, 'April, 1865']