The British first used beige "Khaki" (the Urdu word for dusty) in 1848 by the Corps of Guides in the northwest Indian frontier (now Pakistan). This was part of a general evolution in the army away from its earlier scarlet uniforms, which were already being noted as impractical for skirmishing in the early 19th century. The 95th rifles famously wore green during the Napoleonic wars, and this pattern of using less garish colors for light infantry was not unique to the British. Various Jäger units also employed green, and light blue or grey were also fairly common for light troops trained to fight in loose formation.
For the later half of the 19th century Khaki was used more and more prominently in British colonial overseas campaigns, and in 1902 it was adopted as the British continental service dress.
The Germans came slightly later to the party. As stated above, Jäger and other light infantry regiments had been issued more conservative colors for quite some time, but the move to the grey ("feldgrau") over the traditional "Prussian blue" happened in 1910. By this point modern smokeless powder was being used, meaning that soldiers could no longer count on having a smoke screen provided for them every time the enemy fired. Feldgrau was practical since it blended better with the environment, making it harder to spot both for enemy riflemen and reconnaissance.
A common corollary to this question is why the French were slow to adopt more modern uniforms. The answer is a combination of politics and logistics. France had officially adopted a light blue uniform (actually pretty hard to see) in 1914, but it took some time before these were issued en masse. Earlier attempts to modernize the uniform had met mass political resistance, as the red trousers of the French infantry had become a national symbol of its former military dominance throughout the 19th century until the catastrophic loss to Prussia in 1871.