Russian America was always a bit of an oddball in the Russian Empire. It was the one significant colonial possession (there were some coaling stations in Hawaii) that was not geographically contiguous with the rest of Eurasian Russia, and its administration was in the hands of the Russian-American Company, Russia's first foray into the joint-stock colonial ventures along the lines of the Dutch and British East India Companies (see James Gibson, "Russian Expansion in Siberia and America: Critical Contrasts," in Russia's American Colony, edited by S. Frederick Starr). Furthermore, the indigenous peoples of Alaska (particularly the Tlingit) proved to be much more difficult to subdue than those of Siberia (Andrei Grinev's Indeitsy tlinkity v period Russkoi Ameriki, 1741-1867 gg is far and away the most comprehensive modern account of this, and it's been translated into English as well, as The Tlingit Indians in Russian America, 1741-1867).
Incidentally, the Tlingit today are largely (perhaps even overwhelmingly, though I don't have any statistics at hand) Orthodox--not because of any great success of Russian missionary efforts during the period of Russian rule, but because after the sale to the US the Tlingit found it a useful bulwark against American assimilation (see Memory Eternal: Tlingit Culture and Russian Orthodox Christianity through Two Centuries by Sergei Kan)
So internally, Alaska was not a terribly easy place to hold on to or administer (in addition to the works mentioned above, Ilya Vinkovetsky's Russian America: An Overseas Colony of a Continental Empire gives a broader account of the whole of the Russian experience in Alaska--as the title indicates, it's focused on how to fit Alaska into a general conception of the organization and administration of the Russian Empire). When one considers the foreign-relations situation, it gets even more bleak. By the 1820s, Russian settlement had actually extended as far south as northern California (Fort Ross, now a California State Historic Site, is about 75 miles north of San Francisco). For a while this was a thriving colony built around fur trading and agriculture to supply Alaska, but as the fur trade declined due to overhunting and the RAC reached agreements with the Hudson's Bay Company to bring food to Alaska, it became less and less necessary to the colonial venture and was finally sold to John Sutter a few years before the Mexican-American War (Glenn Farris's So Far From Home: Russians in Early California is a very accessible account of this, and emphasizes indigenous collaboration with Russian settlers as a means of resisting Spanish mission rule in the last years before Mexican independence and secularization of the missions). So through the mid-19th century Russia was retreating back up the Pacific Coast, unable even to effectively police a treaty line that demarcated the limits between Russian, American, and British fur-trading zones, which ostensibly guaranteed Russia exclusive rights north of what is still the present southern boundary between the Alaska panhandle and British Columbia.
And Alaska was in a vulnerable position geographically. The sale took place a few months before "independent" (I'm not familiar enough with British/Canadian colonial historiography to really speak to exactly what the creation of the Dominion of Canada entailed, but my understanding is that it wasn't full independence in the sense we understand it e.g. with the US) Canada was formed, and even then British Columbia and the North West Territory (present day Yukon and more) remained British possessions for a bit longer. Britain and Russia were already rivals for influence in Southwest and Central Asia, and so having two large British possessions butting up right against its own most logistically-inaccessible colony was a strategic issue. Furthermore, Russia had been weakened by its defeat (at the hands of Franco-British-Ottoman alliance) in the Crimean War, and Alaska's marginal economic productivity combined with its strategic vulnerability made it not worth the effort to try and defend.
Finally, in the wake of the Crimean War and the ensuing state crisis, the Russian government enacted a major program of reforms in the early 1860s in an effort to keep Russia competitive on the world stage. The most notable of these was the emancipation of the serfs in 1861, though other reforms included creation of law courts that made serious steps towards adhering to Western norms of rule of law and local government institutions. Part and parcel of this was an attempt (which in the end proved unsuccessful) to regularize administration across the empire--previously, the Russian Empire was characterized by a system of ad-hoc relationships with various subject groups, heavily indebted to the legacy of the era of Mongol rule (Andreas Kappeler's Rußland als Vielvölkerreich, translated into English as The Russian Empire: A Multi-Ethnic Empire is probably the best current work on this subject; see also Karen Barkey's Empire of Difference: The Ottomans in Comparative Perspective for a more abstract discussion of this sort of imperial organization, which focuses on the Ottoman case but takes a comparative approach in which Russia figures prominently). Given Alaska's unique situation, it would have been difficult to regularize in this fashion, and that also militated in favor of casting off Alaska (this is something Vinkovetsky discusses, albeit briefly). More concretely, another aspect of the Great Reforms was infrastructure improvements, and the funds from the sale of Alaska were intended to help finance railway expansion (see Alfred J. Rieber's essay "Interest-Group Politics in the Era of the Great Reforms" in Russia's Great Reforms, 1855-1881, edited by Ben Eklof, John Bushnell, and Larisa Georgievna Zakharova).
So knowing they could not hold on to it indefinitely, selling it in exchange for something was preferable to having it conquered and getting nothing for it. Hoping that the growing American power could serve as a counter to British influence in the region, Russia decided to offer it to the United States. Its first overtures actually came in the late 1850s, but the US obviously had more pressing concerns than purchasing and incorporating a massive territory; by 1867, the situation in the US had changed while Russia still faced the same imperatives it had nearly a decade earlier.