I'm trying to understand how certain men might have been grouped into battles together. For example, were they assigned to different battles by company, regiment, brigade, or some other level of organization? Would they have been grouped purely as a function of distance to battle, or by some other factors like experience or demographic characteristics (region of enlistment, etc)?
The basic unit of organization for the Civil War was the Regiment for both the Union and Confederate armies. Regiments during the Civil war were usually about 1000 men when raised and they were often raised from a specific geographic area, like a small or medium sized town or an ethnic neighborhood of a large city. Regiments could be raised by state authorities, federal officers, or even private individuals who were locally notable and wanted a military career. As such, individual regiments were not recruited in any standard way, and there are a lot of strangely organized ones, especially early in the war. (After conscription I believe there was more standardization but I am not well read enough here to go into it.)
In any case, after a regiment was formed, it came under the orders of the War Department of the USA or CSA. The war department decided where that regiment would go and what army or region it would serve in, although some states had state militia regiments as well as regiments under federal/confederate service. Men of a Regiment typically stayed together until the regiment was released from service or combined with another regiment due to casualties. There was no replacement system until the regiment became too small to be useful.
Regiments were grouped into brigades. Regiments often decreased in size over time due to casualties, or were odd sizes for other reasons. By grouping regiments together in brigades of about 2,000-4,000 men they could be forged into handy tactical units. Quite often, brigades were composed of regiments which all shared the same locality, language, ethnicity or the like. For example the Union's "Irish Brigade" was composed of regiments from New York City and other Irish regiments from other states and there were several German speaking brigades put together along the same lines. Famous CSA brigades like the "Stonewall Brigade" also shared geographic identities, in that case western Virginia. However, not all brigades were organized along those lines, many were put together in an ad-hoc fashion out of whatever pieces of regiments were handy at the time.
Once the War Department released regiments or whole brigades to the local Army Commander (e.g. Army of Northern Virginia, Army of the Potomac, Army of the Tennessee) that Army commander and his staff had great latitude in sorting them out into divisions and corps or reorganizing them into different brigades. This process happened fairly often after heavy casualties were suffered in portions of the army. Occasionally the War Department (or occasionally the president of the CSA or USA directly) ordered large strategic movements of troops from one theatre of war to another, or from one army to a different army. There is no hard and fast rule about what regiments and brigade served where, it was decided based on the immediate needs of the moment.
To summarize, Civil War soldiers were typically recruited from a locality into regiments, and then the war department or the local Army itself decided what to do with the regiment.