The Great Wall near Beijing, the mountainous portions that you speak of, were quite natural at first: they were only built on mountain passes to fill in the gaps between the mountains. However, the northern invaders can always ride around the fortified passes by taking the rougher paths. Then the Chinese would have to build a wall to prevent that, and so on. On a very general level, that's the gist of it.
In the Ming dynasty, when most of the Great Wall we see today are built, the first frontier walls were actually built where they were most needed: the flat loess lands of the Ordos, a strategically important region where the nomads could just ride down and arrive at the western city of Xi'an, the site of many previous dynasties and the "cradle of Chinese civilization". The problem is, the Ordos wall actually redirected the flow of the Mongol invaders around them, making them turn east to strike at the capital region of Beijing, even though it was already blocked by the Yin Mountains.
Normally, the Chinese court can deal with these problems either with punitive military campaigns or by alleviating their need to raid by giving them what they want. But in the case of Ming, both these options are out since the military had faltered during the mid-Ming, and a Confucian disdain of barbarians made giving them what they want hard to swallow, since the Mongols wanted trade and the Chinese court doesn't want its people to mingle with the Mongols. For the military commanders along the frontier, building walls to keep the Mongols out became an acceptable compromise. Soon, wall building became the established way of dealing with the Mongols for the Ming, though it was a only a temporary solution that became permanent.
Beijing, being the capital of the Ming, had to make sure it was amply defended. Not only is an under-defended border dangerous for the dynasty, having the enemy coming so close to the capital is politically risky for the military commander in charge, as the penalty for that is usually death! In 1576, a Mongol raiding party actually climbed through some dangerous terrain to reach a gap in the Wall and killed a number of high ranking officials in a nearby garrison. In light of this, better safe than sorry! This picture was taken near the site of that breach, and you can see the Ming took no chances after that raid, even if they had to wall off a cliff.
Of course, in light of the economic failure that brought down the Ming dynasty, this probably wasn't the best use of funds. The Manchus were able to puncture through the Great Wall from the north up to six times in the last years of the Ming, each time with seeming impunity as there wasn't enough money to station men in the watchtowers on the Great Wall.
Source: