
The war had its roots in the Saur Revolution, a coup in which Afghanistan's communist party took power in the nation in 1978. They initiated a series of reforms within the country, ones which were deeply unpopular in the rural areas. Though the new nation claimed to make the nation a "Democratic Republic", it was quite repressive and it vigorously suppressed opposition and executed thousands of political prisoners. This led to rebellion by many anti-government armed groups by April of 1979.
Within the communist party itself, deep internal rivalries existed between two groups known as the Khalqists and Parchamites. In September 1979, People's Democratic Party General Secretary Nur Mohammad Taraki was assassinated under orders of the party's second-in-command, Hafizullah Amin. This was met with stern disapproval from the Soviet Union, who feared that Amin was planning to switch allegiances to the United States. In response, Soviet leader Leonid Brezhnev ordered the deployment of the 40th Army into Afghanistan on December 14, 1979. The army arrived in the capital city of Kabul, and they staged a coup in which General Secretary Amin was killed. The army installed Soviet loyalist Babrak Karmal from the rival faction Parcham as the nation's new leader.
In January 1980, foreign ministers from 34 nations of the Organisation of Islamic Cooperation adopted a resolution which called for "the immediate, urgent and unconditional withdrawal of Soviet troops" from Afghanistan. The UN General Assembly passed a resolution protesting the Soviet intervention by a vote of 104 to 18, with 18 abstentions and 12 members absent or not voting.
Afghan insurgents began to receive massive amounts of aid, including money and military training in neighboring Pakistan with significant help from the United States and United Kingdom. They were also heavily financed by China and the Arab monarchies in the Persian Gulf. The CIA played a significant role in this by funding military operations. The CIA worked covertly through Pakistani intelligence services.
Soviet troops occupied the cities and main arteries of communication, while the Mujahideen waged guerrilla war in small groups operating in the almost 80 percent of the country, mainly in the rugged, mountainous terrain in the countryside. The Soviets used their air power, levelling villages which they suspected were providing safe haven for the Mujahideen. The Soviets also destroyed vital irrigation ditches, and planted literally millions of land mines.
Afghanistan had been independent during the early stages of the Cold War, until the 1978 coup led by Taraki. The new regime had signed a treaty of friendship with the Soviet Union in December 1978. In April 1979, when Taraki was deposed by Amin, US President Jimmy Carter was surprised by the Soviet invasion. This was contrary to what had been predicted by most of the U.S. intelligence community and Carter had incorrectly believed that Moscow would not forcefully intervene. The CIA officials had monitored the deployment of Soviet soldiers to the Afghan border, but they did not expect the Soviets to launch a full-fledged invasion.
Carter believed that a Soviet takeover of Afghanistan would present a grave threat to the Persian Gulf region. In a televised speech, Carter announced sanctions on the Soviet Union, and he promised renewed aid to Pakistan. In what became known as the "Carter Doctrine," he stated that the U.S. would repel any attempt to gain control of the Persian Gulf.
The Soviet invasion rehabilitated relations between the US and Pakistan. Pakistani leader Muhammad Zia-ul-Haq had previously broken with Carter over Pakistan's nuclear program and the execution of Zulfikar Ali Bhutto. The Soviet invasion of Afghanistan and instability in Iran caused the Pakistani leader to re-evaluate his position and made him realize that it was wise to reestablish Pakistan's alliance with the United States. Carter used Saudi Arabia and Pakistan's Inter-Services Intelligence as a means of increasing aid to the Mujahideen through the a CIA program known as "Operation Cyclone."

Carter also later announced a U.S. boycott of the 1980 Summer Olympics in Moscow and imposed an embargo on shipping American wheat to the Soviet Union. Unfortunately the embargo hurt American farmers more than it did the Soviet economy, and the United States lifted the embargo after Carter left office.
The Soviet invasion of Afghanistan brought about and end to the period of detente that had begun with Richard Nixon and continued under Gerald Ford. The United States returned to a policy of containment and drew closer to its Cold War allies. The US increased the defense budget. U.S. support for the mujahideen in Afghanistan would continue until the Soviet Union withdrew from Afghanistan in 1989.
The international community joined in the boycott of the 1980 Summer Olympics held in Moscow. The boycott and sanctions ramped up Cold War tensions and the Soviets retaliated with a boycott of the 1984 Olympics held in Los Angeles.
The Soviets had initially expected to come into the country, stabilize the government under their newly installed leader Karmal, and leave within six months or a year. But the fierce resistance from the guerillas that they encountered changed their plans. They ended up stuck in a war that lasted nine years. By the mid-1980s, the Soviet contingent was increased to 108,800 and fighting increased, but the cost of the war for the USSR was high.
By mid-1987 the Soviet Union, now under reformist leader General Secretary Mikhail Gorbachev, announced it would start withdrawing its forces after meetings with the Afghan government. The final troop withdrawal started on May 15, 1988, and ended on February 15, 1989. This left the Afghani government forces alone in their battle against the insurgents, which continued until 1992, when the former Soviet-backed government collapsed.
This war has sometimes been referred to as the "Soviet Union's Vietnam." The Soviets' failure in the war and the war's cost are considered by many to be a contributing factor to the fall of the Soviet Union.