.
The basic answer is because the Communist Party in Afghanistan asked them to. Why that is, and why they answered the request, are a bit more complicated. The summary of that, as simple as can be stated, is that the Soviets did not expect nor plan to be in Afghanistan for a decade; or they saw something worth the fight, no matter how long it took.
The Soviet War in Afghanistan was much more a proxy war between the Soviet Union and Pakistan, than it was between the USSR and USA.
#The Russian Ice-Lock
This is going to seem rather irrelevant at first, because Afghanistan is a landlocked country, but this will be important later when I give a final assessment. Russia, since its time as an Empire, has always been plagued by a constant geopolitical problem. This is the fact that Russia does not have unrestricted access to the ocean. For most of the year Russian ports are either completely ice-locked or only grant access to the ocean through NATO controlled straits. For example, the Baltic and Black Seas through Turkey and Denmark. This geopolitical problem has been the source of many long and short term maneuvers in Russian history, especially while the Soviet Union.
#The Great Game & Durand Line
Back during the time of the Russian Empire, the Russian and British Empires played “the Great Game” in Afghanistan and Central Asia. From 1830 to 1895, the two would battle for control. The Russian Empire ended up in control of everything North of Present Day Afghanistan and the British controlled Present Day Indian, Pakistan, and Afghanistan. Note: Afghanistan was, ostensibly, independent but its foreign affairs were controlled by the British. As part of this arrangement, a new border for the state of Afghanistan was established in 1893. This border, called the Durand Line cut Balochistan in half, granting the side with access to the sea to British Pakistan. This agreement was originally temporal in nature, set to expire in 1993 (according to some; this is debated), but the independence of Pakistan complicated matters. Regardless of the expiration or lack of, the British controlled Afghanistan at the time of this "negotiation" and modern Afghanistan refuses to recognize it as the border.
#Third Anglo-Afghan War
In 1919, while the Soviet Union was in its infancy, British India and Afghanistan went to war for the third time. The Soviet Union sent material and arms aid to Afghanistan to help in this battle. Some Soviets saw this as a continuation of the Great Game, some saw it as an effort to resist Capitalist Imperialism; regardless, the Soviets sent aid. Soviet-Afghan relations were off to a strong start. Afghanistan won the war, regaining independence and control of their own foreign affairs. With this new control of foreign affairs, the Afghanistan Monarchy would get closer and closer to the Soviet Union. Over the 1940s and 1950s the Soviet and Afghan militaries would become more inter-connected.
#1973 Coup
From 1747 until 1973, Afghanistan was ruled by a Monarchy. The last ruler of this Monarchy was King Mohammad Zahir Shah. While in Italy for a medical procedure, a coup was carried out by the military. The leader of the Afghanistan Army was Lietenant General Mohammed Daoud Khan. From 1973-1978 he would rule Afghanistan as President. King Shah did not attempt to reverse the coup and instead simply remained in Italy and abdicated. Daoud Khan established a Republic, with political parties. This opened up new opportunities for the Soviets.
Daoud Khan was a hard-line supporter of the Pashtunistan ideology; a nationalism that refused to recognize Pakistan as a country. Pashtun people, who live on both sides of the border, often disregard the border in general and Daoud Khan supported this. This angered Pakistan, naturally. Pakistan’s response was for Inter-Services Intelligence (ISI) to establish Islamist Militant organizations in Afghanistan, in addition to kidnapping and extradition of wanted Afghan and Pashtun officials. Jamiat-e Islami (JEI) became the first Islamist organization in Afghanistan and in 1975 they attempted an overthrow of Daoud Khan’s Republic. This overthrow failed, but began a latent insurgency. Islamist resistance to Daoud Khan’s secularism was rising.
Daoud Khan maintained a relationship with the Soviets, though he had concerns. The Communist Part of Afghanistan was fractured in two factions: Khalq and Parcham. But the Soviets were working to unite them. This was dangerous to Daoud Khan and he argued with Leonid Brezhnev to cease these efforts. Brezhnev attempted to diplomatically avoid addressing the matter, bringing up NATO influence in Afghanistan, but Daoud would have none of it. He told Brezhnev the Soviets would never rule Afghanistan. Daud Khan would launch a purge of the Communist Party and in 1977 established a one-party government. Naturally this was unacceptable to the Communists…
#Saur Revolution
A little over a year after establishing the one-party system, in April 1978, the Communist Party of Afghanistan launched the Saur Revolution; another coup. Daoud Khan and his family were murdered and the “Democratic Republic of Afghanistan” was established. The trouble for the Communists were that Daoud Khan’s opponents, the Islamists, weren’t opposed only to Daoud Khan. The Islamists, such as JEI, were opposed to secularism in general and the changing of the way of life in Afghanistan; the Communists were even worse offenders. Between 1978 and 1979, somewhere in the neighborhood of 30,000 Afghans would be arrested and executed by the government. Kunar Valley (notorious to this day) went into open rebellion and by summer 1979 almost the entire country saw fighting. The Communist Party in Afghanistan would appeal to the Soviet Union for aid and the Soviet Union of course answered.
Pakistan, via ISI, would continue supporting the insurgents and would lobby the United States to join them in doing so. According to Robert Gates, the Carter Administration wanted to salvage its relationship with Pakistan in light of the Iranian Revolution, and this was ultimately the deciding factor into the American involvement.
#The Long Term, What-Ifs, and an Assessment
Why did Pakistan care so much about continuing to oppose Soviet influence in Afghanistan? Why did the Soviets care to unite the Communist Party in Afghanistan? Why be willing to fight a war for ten years?
While most historical analysis on this event are focused on the collapse of the Soviet Union, the rise of exported Islamic extremism, historical what-ifs about Al Qaeda and 9/11, I believe there is a more interest what-if hidden in this decade from 1979-1989. What if Pakistan stopped supporting the Islamists after Daoud Khan’s death? Why didn’t they?
This is where the ice-lock and Durand Line come into consideration. Pakistan responded to Daoud’s attacks on their sovereignty and their territorial holdings. I think the evidence says, even if no one has admitted, that Pakistan was concerned about how the Soviet’s might address the Durand Line come 1993 if they had held sway in Afghanistan. The return of Baluchistan to Afghanistan, which the Soviets could have, and probably would have, lobbied for would have crippled Pakistan and finally broken the Russian Ice-Lock dilemma.
The fact the Soviets were invited, the fact it was to further Communism; that is enough for the Soviets to invade. The fact Daoud Khan threatened Pakistani sovereignty; that is enough for ISI to get involved. But what kept them both opposing one another even later? I think it was the Durand Line.
#References
Ghost Wars by Steve Coll.
Directorate S by Steve Coll.
Unity, Faith and Discipline: The Inter-Service Intelligence of Pakistan by Hein Kiessling.
The Bear Went Over The Mountain by Lester Grau.
The Other Side of the Mountain by Ali Ahmad Jalali and Lester Grau.
The Soviet-Afghan War: How A Super Power Fought and Lost by Lester Grau.