It was a combination of reasons:
- The Soviets were facing an insurgency against an unpopular Afghan government. This was an inherently difficult type of conflict to win, all the more so since the Soviet military was better suited for fighting a more conventional war. The Soviet allies in Afghanistan were corrupt and unreliable.
- The mujaheddin had bases, training camps, and received their supplies from Pakistan. Since the USSR couldn't invade Pakistan, they couldn't really stop the flow of fighters and weapons from across the border. The mountainous nature of the border made it all but impossible to seal. The mujaheddin were trained and supplied by the US and Pakistan, and the Soviet Union couldn't do much about that.
- Speaking of mountains, the terrain in itself presented a challenge. It made it very easy for the insurgents to hide or set ambushes. One common technique was to wait until a Soviet column was the middle of a narrow mountain pass, and then shoot and disable both the first and the last vehicle, trapping the entire column. Even Soviet equipment wasn't suited for the terrain. For example, on certain combat vehicles, the guns couldn't be elevated to a high enough angle to shoot into the mountains.
- In addition, the Soviets never really put a very high priority on the war in Afghanistan and didn't commit sufficient resources. Total Soviet forces in Afghanistan were much smaller, for example, than the analogous American military commitment to the war in Vietnam. The 40th Army, the main Soviet formation in Afghanistan, had a lot of outdated equipment compared to Soviet troops stationed in Europe.