The Soviet War in Afghanistan was a nine-year period involving Soviet forces and mujahideen rebels fighting to overthrow the Marxist People's Democratic Party of Afghanistan (PDPA) government in Afghanistan.
The Soviet Union supported the government while the rebels received support from various sources including the United States (in the context of the Cold War) and Pakistan. The conflict was a proxy war between two superpowers, who never actually met in direct confrontation.
The actual reasons why the Soviet Union invaded Afghanistan are far more complex. The Soviet Union found itself almost completely surrounded by enemies. To the west stood Western Europe, full of NATO forces, nuclear weapons and American bases.
To the southeast was China, which had about a million soldiers on the border with the USSR. Up to this point, relations between China and the USSR were dire, often fatal skirmishes across the Ussuri River, and China was openly working with the United States to contain the Soviet Union by 1979.