A full answer to this question would depend on which soldier you asked at which time in the war. Generally speaking, soldiers in the Union Army tended to say that they were fighting to preserve the Union. Later in the war, however, as the purpose of the war was changed by the Emancipation Proclamation, itself a result of the scores of slaves that had flocked to Union lines, some Union soldiers began to feel they were fighting for a somewhat higher purpose, i.e. the freeing of slaves.
Some evidence suggests a similar, but inverse, pattern in the minds of Confederate soldiers through the course of the war. Many at the beginning of the war wrote of states' rights, or simply self-defense, as reasons for fighting. By the end of the war, however, many were coming to believe that they were fighting for a way of life that included, explicitly in some cases, slavery, even for the majority of soldiers who didn't come from slaveholding backgrounds.
In both armies, however, many soldiers were simply fighting for what soldiers have probably always fought for. They wanted to stay alive and to help their friends. The bonds formed in combat were as strong in the Civil War as in any other conflict. It is also important to remember that, by the end of the war, a majority of soldiers in both armies were conscripts who probably didn't feel that they had much at stake in the war. Major cities in both the Confederacy and the Union experienced draft riots, and as the privations of the war grew worse, scores of Confederate soldiers simply deserted and went home, an offense that was punishable by death.