Why does the path of the Great Wall of China bend back and forth so much? Naïvely, it would have taken less material and guards to defend if it had taken a straighter path, so there must have been some reason. But what?
The Chinese designed the wall to be an effective barrier; that was the goal. And to answer your question, we need to ask: what land barrier stopped foreign troops the best?
The answer in China, and everywhere else, is Mountains. The Great wall is nothing more than a fortification of existing natural barriers. Look closely at the pictures you've posted, and those posted by Ken Graham. You'll see that they built the great wall directly on the mountain ridge. So this is the easy reason the route is bendy:
Because it follows a mountain ridge
I don't know about you, but I wouldn't enjoy building a wall along a mountain ridge. It was a ton of work just to survey the land, and clear trees in both directions from the construction site, and then carry the materials to the site. The expenditure of materials might have been the least costly component.
All this being said, the the real barrier was the mountains. On these mountains, the wall served 3 major functions:
To fortify the mountain passes, yet allow trade to continue
At the passes, the wall was actually the primary barrier. The wall in passes are very high, with multiple wings.
To provide a location for watchtowers at regular intervals.
These watchtowers were not just for watching the terrain north for gathering troops, but also for communications between towers.
Logistics: To provide a direct path between mountain pass fortifications for the transfer of troops, equipment, food, and other materials.
When I've hiked the wall, I found it very difficult, but far easier than traveling along a natural mountain ridge. I think the men who ran along the wall wearing armor and carrying weapons were strong men indeed.
The Great Wall of China:
It is the longest man-made construction in the world. In the old times, it was of great military importance of preventing the enemies' intrusion and was regarded as the 'Guardian Angel' of the central plain in the past. - Great Wall of China FAQs
The Great Wall of China is the longest man-made structure in the world, stretching 21,196.18km (13,170.6956 miles) long. It was built to keep out raiding parties of nomadic tribes, such as the Mongol, Turic and Xiongnu, from modern-day Mongolia and Manchuria. The first Emperor of China, Qin Shi Huang, is often considered the father of the Great Wall, but even before he united the nation in 221BC, individual states built walls to keep out invaders as early as the 7th Century. Qin connected, lengthened and fortified the walls to protect the northern border between 221-206BC. Subsequent dynasties, most notably the Ming, maintained and rebuilt it.
Always maintained as a military defence – at its peak the Ming Wall was guarded by more than one million men – the Wall evolved other uses. Aside from being a transportation corridor, it was used to regulate trade, such as collecting duties on goods transported along the Silk Road. It was also used to restrict both immigration and emigration.
But why does the Great Wall of China follow such a bendy route?
The wall was generally built at the boundary line of the day. In normal conditions, taking the commanding position was the most important factor to win a battle. In this case, lofty mountain top was always the preferred location to build the wall. - Great Wall of China FAQs
High ground is also easiest to defend. As noted before, it was built in top of the ridges. This offers a defensive advantage, as well as good visibility.
Mountain roads are "best" built along ridge lines that minimize (to the extent possible), the surface area of the mountains containing the roads to minimize the necessary excavation. This, in turn, is dictated by the process of building the road through the parts of the mountain, where the slopes are the "minimum."
At its heart, the Great Wall is just a long "road" with walls on both sides. You can see from the pictures that the mountains on either side tend to have steeper slopes than the ridges through which the wall is actually built. These ridges are the points of minimal slope changes. (Think of these slopes as "hypotenuses" over the x axis with the ideal slope being zero.)
A path that was "straight line" (as the crow flies) would usually violate the above rule, and therefore be inefficient in a real environment, even though it would look "optimal" on a map. So a wall with appropriate "bends" will "take less material and guards to defend [than] if it had taken a straighter path."