If so when was Wei closest to defeat?
I recently moved to China and have recently read up on Chinese history and been watching Chinese historical dramas such as "Three Kingdoms" which got me really interested in the period.
Also, considering that Shu controlled a lot less territory than Wei and Wu how did they manage to survive so long and even go on the offensive several times?
No. Shu Han was never in a position to defeat Wei, despite what Romance would have you believe. You might also be interested in my previous posts about the myths of Zhuge Liang here and here.
Take a look at the map of three Three Kingdoms and you will understand why. Just from the concentration of cities, you can automatically tell that Wei territory was much more populous. The North China Plain, prior to the Song-Yuan transition, was the most populous and productive region in all of China. By contrast, large expanses of Wu and Shu territory were undeveloped and populated by indigenous people. It won't be until the Tang that the development of southern China really began. Both Wu and Shu, therefore, lacked the manpower and resources to fight prolonged campaigns against Wei. Zhuge Liang's Northern Expeditions were all designed to be swift and decisive, and each time he failed because of supply constraints as his campaigns were bogged down. While the Northern Expeditions did put Wei on the defensive, at no point did it seriously threaten the survival of the Wei.
Then there was the fact that the Shu lacked decisive leadership after Liu Bei's death. His son was, by all historical accounts, completely inept and uninterested in state affairs. While Zhuge Liang did serve as regent, his rule was, of course, not absolute as he was only ruling on behalf of the emperor and had to ultimately answer to the emperor. After Zhuge Liang's death, no one person held control of both civil and military affairs. Zhuge Liang's son Zhuge Zhan controlled civil matters while Jiang Wei became the de facto leader of the military. Zhuge Zhan wanted to consolidate Shu Han territory and focus on internal development while Jiang Wei advocated for an aggressive policy against the Wei. There was a lot of political bickering within the Shu court. Jiang Wei's expeditions against Wei similarly failed due to inadequate supplies or defeats, and his campaigns drained whatever limited resources Shu had, leading to its eventual collapse.
Shu managed to survive that long mainly because of its alliance with the Wu, as these two weaker states realized that if one of them fell, the other would be next. In fact, Cao Pi gave up a heaven-sent opportunity to reunite China in 221, when Liu Bei attacked Sun Quan. Sun swore fealty to Wei and became a vassal, but as soon as Liu Bei was defeated, Sun Quan reneged on the agreement. Had Cao Pi launched an invasion of Wu in tandem with Liu Bei, the Three Kingdoms could have very well ended with an eventual Wei victory.