There are two main approaches one should take when discussing the history of relations between the Soviet Union and Afghanistan. One approach focuses on the historical relationship between Czarist Russia and Afghanistan and emphasizes the former's long-time search for a warm-water port, Russia's coastlines being subject to intense freezing during the long winter months of the Arctic and even in the Pacific, where the Sea of Okhotsk is similarly frozen for much of the year. Control of Afghanistan does not in and of itself remove such restrictions from Russia's maritime calculations, but it does place them thousands of miles closer to the Persian Gulf and Arabian Sea, which are navigable all year round. So intense was Russian interest in Afghanistan, and Central Asia in general, that the competition for influence in that vast region between Russia and another great imperial power, Great Britain, resulted in extreme maneuvering for geopolitical advantage that became known as "the Great Game."
Russian interest in Afghanistan, while serious during the reign of the czars, grew following the end of World War II. The early post-revolutionary period in Russian history -- the Bolshevik Revolution -- saw Soviet leaders too preoccupied with their efforts at consolidating the old Russian colonial holdings in Central Asia and in Russia's Far East to be able to focus much on Afghanistan. The Soviet Army, while populated with some brilliant military minds, was too weak prior to World War II to project the kind of military power beyond its borders necessary to be able to compete with British and, subsequent to the war, US power. The Soviet Army that emerged from the war against Nazi Germany, however, was one of the two most formidable militaries in the world, second only to that of the United States. This ability to project military power, especially over land, to neighboring regions changed Soviet-Afghanistan relations.
Facilitating the growth in Soviet influence in post-war Afghanistan was the demise of the British Empire, particularly the end of British colonial control of the Asian Sub-continent (i.e. India/Pakistan). The British departure left a great power vacuum in the region that now pitted the Soviet Union against the growing US interests in the Middle East and South Asia. The Soviet Union began to use its intelligence service, the KGB, to manipulate events inside Afghanistan, supporting pro-communist Afghanistani officials against those loyal to the monarchy. The 1953 ascent of General Mohammed Daoud Khan, a relative of Zahir Shah and ally of the Soviet Union, provided an open invitation to the Soviet Union to greatly expand its presence and interest in Afghanistan. The king remained in power, but it was his cousin who would increasingly pull the strings.
While the government under General Daoud Khan was pro-Soviet Union, it wasn't particularly in line with the official ideology of the USSR, and the Soviets grew increasingly suspicious of the general when he sought closer ties to the United States. Soviet-Afghan relations became considerably more complicated with the emergence of the Afghan Communist Party during the mid-1960s. That party, under the leadership of Babrak Karmal and Nur Mohammad Taraki, would subsequently compete with the king and with General Daoud Khan for influence and power within Kabul. The general's 1973 coup ended the monarchy and Daoud Khan imposed a more autocratic form of government with close ties to the Soviet Union. From that point on, Afghanistan would remain subject to constant political warfare between the government and the communists, with the latter prevailing following the April 27, 1978 Saur Revolution, the communist coup that ended the reign of General Daoud Khan and replaced it with a communist regime that was ironically less inclined to tie Afghanistan to the neighboring colossus, and that remained mired in internal strife. The newly established communist-ruled Democratic Republic of Afghanistan would keep relations with the Soviet Union cordial, including the signing of a "friendship and cooperation" treaty, but fighting within the ruling Communist Party would prove fatal for everyone. A leader of the 1978 coup, Hafizullah Amin, rebelled against President Taraki in a power struggle.
While all of the above was going on, the role of Islam in traditional Afghanistani culture remained very strong, and many of those who revered their religion grew more militantly opposed to the communist regime, which was officially atheistic. As militant Islam began to grow and take hold across Afghanistan's vast countryside, the political turmoil within the communist party continued to rage. President Taraki's murder on September 14, 1979, at the hands of Amin supporters further destabilized an already unstable government. Fearing the loss of control, and seizing an opportunity to consolidate its hold on Afghanistan, which would move Soviet military assets to within a very short distance of the enormously important Strait of Hormuz in the Persian Gulf, the Soviet Union launched its fateful invasion on Christmas Eve.
For the next ten years, Soviet-Afghanistani relations were defined by the former's support for the nominally-ruling Afghanistan Communist Party and by the growth of the U.S.-Saudi-sponsored insurgency against the Soviet occupation. The eventual Soviet defeat and decision to withdraw its military from Afghanistan 10 years after its invasion marked the end of the relationship between those two nations. The guerrilla insurgents who, with considerable Western assistance, forced the Soviet Union to withdraw from Afghanistan proved incapable of governing, as ethnic and political factions that remained entrenched among the population were too much to overcome -- until the emergence of the Taliban movement.
Since its withdrawal from Afghanistan, there has been no relationship between it and the Soviet Union/Russia. Taliban rule left Afghanistan seriously isolated internationally, with no country confident of how to deal with the extremists who comprised that movement-turned-government. The US invasion of Afghanistan following the terrorist attacks of September 11, 2001, further minimized the prospects, at least short-term, of a reinvigorated Russian presence there. While many Afghanistanis are not as hostile to Russia as they were to the communist Soviet Union, they remain wary nonetheless of a potential resurgence of the old Russian historical interest in the Indian sub-continent and in Central Asia (which had been part of the Soviet empire prior to its dissolution).