What defenses did Lee leave behind when the Army of Northern Virginia left for Gettysburg?
Lee never had the troops to safeguard Richmond much less safeguard Richmond and conduct offensive action, such as his invasions into the north (1862, 1863). When Lee took over command of the Army of Northern Virginia(June 1, 1862) Union General McClellan was at the gates of Richmond and outnumbered Lee's forces. McClellan also had additional troops traveling overland which would significantly add to his numerical advantages. Lee was a master strategist and relied on his gyle and ability to read his opponent to safeguard richmond. Thus Lee did not leave any significant force in Richmond. He couldn't afford too, nor would any forces he did leave be sufficient.
Lee took command of the Army of Northern Virginia June 1, 1862, when General Johnston was wounded at the battle of Fair Oaks, May 31, 1862. Lee effectively swept the Union army out of Virginia with the Seven Days Battle(june 25 - July 1, 1862) and the Second Battle of Manassas(Aug 29th 1862).
In Lee's breakout of Richmond he sent Stonewall jackson to engage and draw off McClellan's reinforcements coming overland from Washington D.C. Jackson's Shenandoah campaign is widely regarded as the finest display of tactics in the history of the US army. Jackson marched his men hundreds of miles up and down the Shenandoah valley at times engaging 3 union army corps occupying 50,000 men denying them to McClellen. Next Lee sent JEB Stuart's calevery out to harass McClellen. In this exercise JEB stuart road completely around McClellan's army. During this exercise Stuart was at all times just a few miles from McClellan's HQ, but McClellan known for being fastidious with battleplans was not able to respond in a timely manner to stop Sturt's few hundred man assault on his 100,000+ force.
McClellan, denied reinforcements by Jackson and embarrassed by Stuart next would be assaulted by Lee, Lee would deny McClellan time to plan. In a series of rapid battles over Seven days, Lee would engage McClellen in 8 battles and walk him north. Ultimately McClellan would withdraw to Washington so rattled was he, that he was unable to conduct his own strategy, but rather just able to react to Lees moves.
Next would come the Second Battle of Bull Run, defeating General Pope. Lee again would be outnumbered, but defeated Pope and concluded sweeping the Union out of Virginia (August 29-30 1862).
Lee did not leave any substantial forces defending Richmond because all his forces would have been insufficient an numerically inferior to the Union forces that could have been arrayed against them. Lee forced the Union north, and invaded the North, threatening Washington to make the Union follow him to safeguard its capital.
Once Union Armies were out of Virginia, Lee invaded the North to make them focus on him, rather than threaten the Confederate Capital. Which is what happened. The Union forces met and strategically defeated Lee's first invasion of the North at Antietam (Sept 17, 1862). Lee retreats back into Virginia.
In May June of 1863 when Lee began his second invasion of the North ultimately being turned back at Gettysburg he again left minimal defenses at Richmond for the same reasoning. In invading the North Lee was gambling / dictating that the war would be fought on northern territory, and that same territory would bear the brunt of maintaining both armies. The North could not venture south while Lee was threatening the union capital.
One of Lee's greatest gifts in the Civil war was understanding his opponent and dictating their moves. His grasp of strategy and his opponents is what made him exceptional.