This is a question that requires a whole book to answer and historians will never know exactly how Stalin rose to prominence. So keep in mind what follows is a very rough sketch of the "grey blur" that was Stalin in his rise to power.
Stalin's rise to power after Lenin's death appeared at first highly unlikely. One of the keys to Stalin's ascent was his willingness to do the bureaucratic drudge work of the Politburo that was less romantic and more labor-intensive. This meant Stalin had a great deal of political capital in the factionalism that arose during Lenin's illness and death; having Stalin on your side meant having some of the weight of the Soviet bureaucracy behind you. Hence he was courted by both the left and right wing factions of the Bolsheviks. This drudge work also meant that Stalin achieved a level of practical experience in dealing with political rivals and enemies. While other Bolsheviks were more learned in Marxist ideology, Stalin began to have a better appreciation for the individual psychology and mentalities of his compatriots. Stalin's relatively crude manners and alleged anti-intellectualism also led more sophisticated Bolsheviks like the urbane-minded Trotsky to underestimate him. Stalin's ideological flexibility and willingness to be ruthless made him even more dangerous.
Lenin of course grew to dislike Stalin and the former's last testament advised that Stalin be stripped of his position in the Politburo. Although later Trotskyites would claim this was a sign of the incongruity of Stalin to Lenin's grand design, Lenin's last testament also critically evaluated other members of the Politburo, calling out Trotsky's arrogance for example. Stalin also was able to subtly play on the antisemitism still common in Party circles to eliminate prominent Jewish-born rivals like Trostky, Zinoviev, and Kamenev.
Naturally, rising to power is not the same as keeping it (the British PM Walpole famously likened executive power as "climbing the greasy pole") and what helped Stalin retain his power was that his reverse course (going from an opposition to Trotsky's radicalism to embracing a crash industrialization of the first Five Year Plan) of 1928 was very popular in many leading Bolshevik circles. Although the NEP era had created a degree of stability and prosperity in some quarters of Soviet society, its crass consumerism and the presence of such problems like unemployment stuck in the craw of many dyed-in-the-wool Bolsheviks. According to their logic, allowing NEP to continue was tantamount to abandoning the dream of the USSR being a beacon for world revolutions. The dream of this new society was quite popular among the Soviet public (at least in the cities), as evidenced by the new Soviet popular culture that emphasized the proletarian protagonist. such as Vasily Chapayev. The Soviet police had increasingly begun to see massive social problems in Soviet cities like vagrancy and there was wide-spread pressure from below for the state to enact new methods to control this population. In short, there were a number of quarters within the Soviet bureaucracy and public that welcomed the new course that Stalin had adapted.
One final note about Stalin's dictatorship is in order. Although from the outside, it appears to be a classical archetype of the cult of personality, especially given Stalin's eel-like ability to slip into new ideological positions. Stalin though would likely have not seen it in those terms. One anecdote exemplifies this. When Stalin's son Vasily used his father's name to escape the repercussions of one his drunken benders, his father was furious. Vasily defended himself by saying "I'm a Stalin too!," to which his father replied, "You're not Stalin and I'm not Stalin. Stalin is Soviet power. Stalin is what he is in the newspapers and the portraits, not you, not even me!'"
Sources
Side note, Stephen Kotkin has a new Stalin biography coming out next month and it should further illuminate this question better than my (poor) answer.
Brandenberger, David. Propaganda State in Crisis: Soviet Ideology, Indoctrination, and Terror Under Stalin, 1927-1941. Stanford, Calif: Hoover Institution, Stanford University, 2011.
Hagenloh, Paul. Stalin's Police: Public Order and Mass Repression in the USSR, 1926-1941. Washington, D.C.: Woodrow Wilson Center Press, 2009.
Plamper, Jan. The Stalin Cult: A Study in the Alchemy of Power. Stanford, Calif: Hoover Institution, Stanford University, 2012.
Weeks, Theodore R. Across the Revolutionary Divide: Russia and the USSR, 1861-1945. Chichester, West Sussex: Wiley-Blackwell, 2011.
There are a whole bunch of reasons why Stalin was able to become defacto leader.
Stalin held a number of key posts within the Party machinery that gave him considerable power. Being General Secretary meant Stalin was able to control the way certain policies were implemented and to an extent control the 'agenda' of the party - since the Secretariat did things like schedule meetings, handle the minutes and other paperwork these meetings generated and so forth. The importance of Stalin being in the Orgburo is often understated - probably because the Orgburo was roled into the Secretariat in 1952. Being on the Orgburo meant Stalin could control who got what jobs in the party, particularly at the local level. Stalin was in charge of Rabkrin prior to being General Secretary, and was another office where Stalin could scrutinize and pressure parts of the State apparatus.
Stalin was good with alliances. Stalin had backed Lenin in a number of key debates, including against Trotsky. Stalin was trusted enough with Lenin that he was effectively overseeing Lenin's period of convalences as he grew ill. Stalin's support of Lenin and his power as a 'weapon' against opposition groups was half the reason Stalin had all those powerful offices in the first place. Stalin had his own allies, like Sergo Ordzhonikidze, who took over in Rabkrin when Stalin move on to bigger and better things. Being Commissar of Nationalities meant Stalin was able to come into contact with party members in marginal areas who might otherwise have been ignored by the Centre - but all those 'marginals' still had a fair bit of collective pull over the Centre on some issues. By all accounts Stalin was actually quite personable and accessible to people in the party which did him a lot of good. Stalin made allies, first on the 'left' and later on the 'right' during the 1924-1927 period approximately - during this period Stalin was often depicted as just a member of a team. When the struggles started over Lenin's replacement, Stalin had a lot of supporters which protected him for some of the bad things (like Lenin's Testament) and gave him support to defeat opponents.
The other half of why Stalin had those jobs is he was a decent administrator. In a party full of ideologues, Stalin demonstrated he could run things. Aside from winning him useful positions and making him effective in them, it made Stalin an appealing choice as a leader - he got things done.
Stalin's ideological positions, while not terribly impressive in their sophistocation of boldness, were easy for the rank and file to follow and were generally seen as pragmatic. It wasn't till after Stalin had secured his position you see the radical swing to collectivisation etc. and even that Stalin sold as being necessary to avert crisis.
The opposition to Stalin just wasn't all that impressive with hindsight. Trotsky was a great orator and made sophistocated arguments etc. but he had few allies (for an array of reasons - people feared his power, he was seen as a snob etc.) and fewer important posts (his big claim to fame was as a leader of the Red Army... but in political terms thats not a great post, and just scared some people in the party). Lenin was of course starting to die when he realised that maybe he didn't entirely like Stalin - part of which may have just been a personal dispute. This greatly limited Lenin's means to deal with Stalin as a threat. Stalin's first major allies, Zinoviev and Kamenev, where not greatly trusted in the party because they opposed Lenin's call of revolution in 1917. On the 'right' you have Bukharin who was well respected, but seen as too young to rule (which I think Lenin pointed out in his 'testament'). So essentially Stalin had things going for him his opponents didn't, so he wasn't just 'strong' - they were 'weak'.
Reference wise - Alan Bullock's comparitive biography of Hitler and Stalin has some surprisingly detailed narrative of Stalin's rise to power. Robert Service and Simon Sebag Montefiore's Stalin biographies readily captures Stalin's personality, ability to build alliances and hence why those were advantages.