From 1862 to December 1863 Foster commanded the Department of North Carolina. After the Emancipation Proclamation, he appointed Horace James, a Congregational minister, to help freedmen prepare for independent life, and directed a former contraband camp to be developed as the Freedmen's Colony of Roanoke Island. By 1864, 2200 freedmen were settled on household plots. Many worked for pay for the Army, which held the forts. Under President Andrew Johnson, after the war, the Army abandoned the colony. Most of the freedmen chose to return to the mainland for work.
Foster was born in Whitefield, New Hampshire. When he was ten, his family moved to Nashua, where he attended the local schools before enrolling in the Hancock Academy. He graduated from West Point in 1846, fourth in his class of 59 cadets.
He first served as an engineer during the Mexican-American War, under Winfield Scott. Severely wounded at the Battle of Molino del Rey, he won two brevet promotions for bravery. After the war, Foster returned to West Point as an instructor. In 1858 he was on engineering duty in Charleston Harbor, where he helped in the construction of Fort Sumter.
Promoted to captain of U.S. engineers, Foster was in command of the garrison at Fort Moultrie when the Civil War began. He immediately transferred his small force to Fort Sumter and became second-in-command to Maj. Robert Anderson during the Battle of Fort Sumter. Foster was appointed a brigadier general of volunteers on October 23, 1861, and commanded the 1st Brigade in Maj. Gen. Ambrose Burnside's North Carolina Expedition. He was conspicuous in action at the battles of Roanoke Island and New Bern. After the Battle of Roanoke Island, the Union renamed the Confederate Fort Bartow as Fort Foster in his honor. The US Army dismantled the three forts on the island in late 1865 after the end of the war.
After General Burnside was transferred to Virginia, Foster assumed command of the Department of North Carolina. He was promoted to major general of volunteers on July 18, 1862, and led the Goldsboro Expedition. During Confederate Lieutenant General James Longstreet's Tidewater Campaign, upon hearing of a planned Confederate attack on Washington, North Carolina, Foster personally assumed command of its defenses. When D. H. Hill demanded the surrender of Washington, Foster defiantly replied, "If you want Washington, come and get it." Hill's forces besieged the garrison and two Union relief expeditions were turned back. Foster escaped the besieged city in order to personally lead a relief column back. Hill withdrew his forces shortly afterward. Both Confederate and Union forces burned down much of the town. In December, Foster led the Goldsboro campaign won two minor battle at Kinston and Goldsboro against inferior confederate forces led by brigadier general Nathan G. Evans, resulting in the shutdown of the vital Confederate supply line of the Wilmington and Weldon Railroad for merely two weeks.
After President Abraham Lincoln issued the Emancipation Proclamation freeing all slaves in rebel territory, in April 1863 Foster appointed Horace James, an experienced Congregational chaplain, as "Superintendent of Negro Affairs for the North Carolina District." To support the transition to freedom for former slaves, he was to develop a colony from the contraband camp that had developed on Roanoke Island. It became known as the Roanoke Island Freedmen's Colony (1863-1867). James also managed other camps for freedmen in the state, such as one at Trent River near his base at New Bern. Foster directed James to settle the people, give them farming tools and prepare them for a "free and independent community."
By 1864, 2200 freedmen lived on Roanoke Island, settled on individual household plots, and working for pay for the Army. James recruited teachers and founded several schools. A sawmill was built and a fisheries started. More than 150 freedmen from the island joined the nearly 4,000 from North Carolina who joined the United States Colored Troops. After the war, the Army dismantled the forts. Due to President Andrew Johnson's returning lands to Confederate landowners, the colony was abandoned. Most of the freedmen chose to be transported to the mainland, where they looked for work. By 1870, only 300 remained on the island.
In December 1863, Foster was sent to Tennessee to assume command of the Department of the Ohio and its corresponding Army of the Ohio. He was in command for only a short time before he was badly injured in a fall from his horse. Upon his recovery, he took command of the Department of the South and aided in forcing the surrender of Savannah, Georgia. He was making preparations for the surrender of Charleston, but his wounds forced him to relinquish command to Maj. Gen. Quincy A. Gilmore.
Foster was assigned to command of the Department of Florida at the end of the war, receiving a promotion to the rank of major general in the volunteer service and brevet major general in the regular army.
After the war, Foster remained in the army, being promoted to lieutenant colonel of engineers in 1867. Promoted to colonel of engineers in 1871, he was involved in military and underwater surveying. He became an expert in underwater demolition, publishing a definitive manual on the subject in 1869 that became the acknowledged reference work. From 1871 until 1874, he was assistant to the Chief of Engineers in Washington, D.C. His final post was as superintendent of the Harbor of Refuge on Lake Erie.
Foster died in Nashua, New Hampshire, and was buried in Nashua Cemetery. The first official reunion of the New Hampshire Veterans Association, which took place in Manchester, New Hampshire in October 1875, was named Camp J.G. Foster.The John G. Foster Post #7 of the Grand Army of the Republic in Nashua was named in his honor.
In 1900, Fort Foster in Maine was named in his memory by the US Army. It is preserved as a park.