What we now term 'Xinjiang' can more accurately be split into three major subdivisions: the Zungharian Basin to the north, the Turfan Depression to the east, and the Tarim Basin to the southwest. While the former two were taken largely as a result of longer-term geopolitical and ideological aims, the conquest of Tarim was somewhat of an afterthought to that, and that is largely reflected in Qing policy.
First, a bit of a geographical overview, so we can get our bearings. This topographic map shows most of the Xinjiang region. The Zungharian Basin is the valley marked 'Dsungarei', the Turfan Depression is to the south, marked 'Turpan-Senke' in black, and the Tarim Basin is marked 'Taklamakan' in white. Two routes lead into Xinjiang from the east. One is via the Gansu Corridor, which terminates at the Jade Gate, located due south of Hami (on the map, it's just above the 'n' in 'Altun Shan'). The other is via the mountain passes through the Altai range, which split Mongolia proper from the Zungharian and Turfan basins. In general, the region is dominated by cold desert, but there are exceptions. Zungharia and Turfan are relatively fertile valleys, lying along major rivers, while the edge of the Tarim Basin is lined with oases, fed by runoff and glacial melts from the Tianshan, Pamir and Kunlun ranges.
As the Ming Dynasty in China disintegrated into various feuding pretender successor states and peasant rebel confederations, to be mopped up by the advancing Manchu Qing, politics among the Mongols were in quite an interesting position. During the early seventeenth century, the Mongols were broadly divided between two confederations: the Four Oirat in the west, and the Khalkha to the east. These two confederations were more or less locked in constant conflict, and both also sought Russian assistance in attempting to overthrow the other. However, such assistance was not initially forthcoming, and in some instances Mongol tribes attacked Russian forts in an attempt to loot weapons, particularly firearms, for use against their enemies. The Russians, who lacked the landward power projection to truly challenge Mongol power, responded by trying to establish more regular diplomatic relations to avoid further military confrontation, and worked towards playing the two sides each other. By 1630, the Torguts, a key Western Mongol tribe who had been the most forthcoming in seeking Russian assistance, actually migrated all the way to the banks of the Volga to escape the inter-Mongol conflicts, but without having to outright declare submission to the Tsar – some of their descendants remain in the region and are known as the Kalmyks.
The rising power of the Russians to the west and north did lead to a gradual, but inconsistent cooling of the internecine conflicts of the Mongols. A conference of the Oirat princes in 1616/17 led to a mutual agreement of non-aggression between the main Oirat tribes (the Khoshut, Torghut, Dörbet, Zunghar, Choros and Khoit), and in response to Khalkha attacks the Oirat Khan, Baibagas of the Khoshut, was ultimately able to assemble an army of most of the tribes, save his major rival, Khara Khula of the Zunghar. It would not be until the early 1620s that the Oirat tribes fully united against the Khalkha, defeating Altyn Khan in 1629, but even then the Oirat were racked by internal divisions, and fought a brief civil war in 1625. However, Baibagas and Khara Khula's successors, Gush Khan of the Khoshut and Batur of the Zunghar, had markedly warmer relations, with Batur holding the title of Hongtaiji, nominal second in command to Gush. In reality, Batur was the real holder of power, but the convention that only a descendant of Genghis could hold the title of Khan meant that he could not officially claim it. The united Oirat achieved victories over the Kazakhs and the Tibetans in the mid-1630s, but during this time many of the Khoshut, including Gush, migrated to Kokonor (Qinghai) and established an independent khanate there. Nevertheless, Batur managed to exercise some uniting influence, and at a great assembly in 1640 the main Mongol concentrations – the Khalkha, the Kokonor Khoshut, the Volga Torghut and Batur's Oirat – agreed on mutual non-aggression, unity against external threats, and measures for resolving inter-tribal disputes. Batur opened trade with the Russians and established a capital at Kubak Zar. However, his death in 1653 caused the loose confederation he had built to collapse.
His son Galdan, however, was able to rapidly reunite the core tribes of the Oirat – besides those on the Volga and the shores of Kokonor – into a new force, the Zunghar Khanate. This represented a very different sort of state formation compared to the Mongol tribal confederations before it, in a few key ways. First of all, the Zunghars built up a major agricultural base in the eponymous valley, rather than relying solely on trading with, raiding or conquering adjacent agricultural bases such as the Tarim oases or the Ordos Plateau, signalling a move away from traditional nomadic empires. Secondly, Galdan's recognition as 'Boshoghtu Khan' by the Dalai Lama allowed him to claim supreme rule over the Mongols, by apparent divine mandate, without being of the Chinggisid lineage, and gave the Zunghar not only an ideological justification, but indeed an imperative, for establishing this supreme rule. By the 1680s, Galdan was in a position to actively challenge the Khalkha, and while his invasion of eastern Mongolia drove many into the hands of the Manchus and the Russians, to whom more than a few submitted and became subject, for a brief period, Galdan's supremacy over the Mongols (outside Tibet, now ruled by the Khoshut) was undisputed.
The Kangxi Emperor in Beijing had good reason to be not only wary, but indeed outright hostile. If the Zunghar were able to fully unite the Mongols, then the state they produced might prove to eventually be a threat to the Manchus' conquests in China, especially should they join forces with the Russians. Moreover, the Zunghar would be competing with the Manchus for the loyalties of several Mongolian allies of the Manchus, including the Chahar and Kharachin. The 1689 Treaty of Nerchinsk specifically disallowed Russian cooperation with the Zunghars as a condition for allowing them to sell furs at Kiakhta, and in the 1690s Kangxi began a series of campaigns to drive the Zunghar out of Mongolia. After a minor success at Ulan Butong in 1690 and a decisive victory at Jao Modo in 1696, the Qing pushed the Zunghar out of Mongolia, and Galdan's nephew and political rival Tsewang Rabdan moved to overthrow him, becoming khan upon Galdan's death in 1697.
Despite losing Outer Mongolia, Tsewang Rabdan managed to bring the Zunghar Khanate to the height of its power. Partly, it was through fostering its internal development, encouraging agriculture, crafts and commerce, and partly through consolidating his hold over the Tarim Basin through arrangements with the various Muslim urban leaders. However, his attempt to conquer the Khoshut in Tibet in the 1710s would be foiled by the Manchus, who established their dominion in Tibet in 1720. Controversies over a subsequent revolt in Tibet, blamed on the Zunghar, led to Tsewang Rabdan's successor, Galdan Tseren, coming under heavy attack from the Manchus almost as soon as he took power in 1727. A peace deal would be hammered out between Galdan Tseren and the Yongzheng Emperor in 1735, but Galdan Tseren's death ten years later led to the division of the khanate between two nobles, Dawaci and Amursana. Although the Qing sponsored Amursana as a client and he was able to defeat Dawaci, he rebelled against Qing rule in 1756. He was, however, was swiftly defeated, and in turn the Yongzheng Emperor's successor, the Qianlong Emperor, ordered the eradication of the Zunghars – of an estimated 600,000 Zunghars, 70% either died of smallpox (deliberately transmitted) or were massacred, while the remainder either escaped to Russia or were enslaved by the Manchus. Zungharia and the Turfan Depression thus fell under Qing dominion, as they sought to move colonists and Mongol allies in to repopulate the region.
Turkestan fell to the Qing two years later. Khwājā Jihān, the younger brother of Burhān ad-Dīn, the Zunghars' former client in Turkestan, had led a Turkic contingent of Amursana's army when the Qing suppressed his rebellion, and induced his brother to attempt to stake out an independent state in Tarim before the Manchus could establish dominion, to which he agreed. While the Turkic armies did find reasonable success, once defeating a Qing army besieging Kucha and managing to encircle a second force sent to Yarkand, in the end the Qing were able to achieve victory – Burhān ad-Dīn had lacked support from the key eastern hub at Aksu, and was forced to abandon Kucha for Yarkand and Kashgar relatively early. Ultimately, he never had the material resources necessary to hold the Qing off, and the army outside Yarkand, having had a stroke of luck in coming upon wells and underground grain stores, were able to survive until the arrival of a relief column from Ürümqi.