To elaborate on what happened was that the fortified pass in particular was the highly strategic Tong Pass.The generals originally in charge of defending the pass were Gao Xianzhi(the same commander who led the Tang army at the Battle of Talas) and his deputy Feng Chengqing.The two were extremely upright and managed to offend a corrupt eunuch Bian Lingcheng,who was in the service of the emperor.This particular eunuch began to sow doubts of the two generals' competence and loyalty in the mind of Li Longzhi(the emperor) given these two were just defeated by An Lushan prior to retreating to Tong Pass(the Court also believed that An Lushan was weak and could be easily defeated).The two were executed as a result and a new general,Geshu Han was deployed.Now Geshu Han was a bitter enemy of the corrupt and incompetent Chancellor Yang Guozhong,who owed his position due to him being a relative of Li Longzhi's favorite concubine Yang Yuhuan.Due to the fact that Tang forces assembled around Tong Pass were mostly new recruits or poor quality troops(like the original Imperial guards that defended the capital,which was a largely political force filled with hereditary recruits who often neglected their training or paid some other guy to fill their post,since the force was used to prevent coups rather than expect to fight wars),Tang forces were unsuited to go on an offensive against An Lushan's highly experienced forces,which was the same reason why this army was originally defeated when it was commanded by Gao Xianzhi and Feng Chengqing.Geshu Han knew this and refused to go on an offensive,but due to his poor relationship with Yang Guozhong,who probably tried to get rid of him the same way Bian Lingcheng got rid of Gao Xianzhi,the emperor was persuaded by Yang Guozhong into thinking that the army was adequate for an attack on An Lushan's forces and Geshu Han was ordered to sally forth out of the pass with his army.With the recent example of Gao Xianzhi and Feng Chengqing,Geshu Han did not dare to reject the order and the result was that the army which originally defended the pass was destroyed and the Tong Pass was easily taken by An Lushan's forces now that it's garrison has been largely destroyed.Without the mountains which surrounded it,the only thing that protected Chang'an from An Lushan's forces was the city's walls.Given that much of the forces which originally protected the capital region was now either destroyed or were of poor quality--the result was that the emperor had to flee the city and abandon it.
If you are interested in why the Imperial Guards were largely an ineffectual force,it's largely got to do with the fact that the Tang Dynasty was filled with a ridiculous amount of coups.Up till and including Li Longji's own reign,the only emperor without a military coup during his reign was Li Zhi.Even Li Longji himself ascended the throne through the help of not one,but TWO military coups using the Imperial Guards.Because of this,Li Longji left control of the guards in the hands of close confidents who were loyal but not necessary capable, like Chen Xuanli,who was left in charge of the guard for 45 years after first helping Li Longji come to power in 710.There's also the fact that the much more militarily capable sixteen guards,which largely comprised of part-time soldiers who were given land in exchange ,was largely rendered symbolic after Li Longji reformed the army into a professional force which largely spent it's time on the frontier.This left the capital largely defended by guard units that either comprised aristocrats or those that inherited their positions due to them being descendants of the original imperial guard when Tang first came to power.Quite often,wealthy guardsmen would hire proxies to serve on their behalf rather than actually spend time in their military unit.