FORTUNE — To understand how Samsung — yes, Samsung — became America’s No. 1 mobile phonemaker and thorn in Apple’s side, it’s helpful to rewind to last fall. On a mid-September morning, Apple (AAPL) CEO Tim Cook stepped onto a stage in San Francisco to unveil the iPhone 5. Several hundred miles away, in a Wolfgang Puck restaurant in Los Angeles, a group of marketing executives from Samsung Electronics followed real-time reactions to Cook’s remarks. They huddled around tables mounted with laptops and TV screens, carefully tracking each new feature and monitoring the gush of online comments on the new device via blogs and social media sites. As the data flowed in, writers from the company’s advertising agency, who were also camped out in the restaurant turned war room, scrambled to craft a response.
Two hours later, when Cook stepped off the stage, the Samsung group was already drafting a series of print, digital, and TV ads. The following week — as the iPhone 5 went on sale — the company aired a TV ad mocking Apple “fanboys” queuing up for the new phone. (“The headphone jack is going to be on the bottom!”) The 90-second commercial went on to become the most popular tech ad of 2012, garnering more than 70 million views online. More important, in the weeks following the launch of Apple’s iPhone 5, Samsung sold a record-breaking number of its own signature smartphone, the Galaxy S III. “We knew this was going to be a big moment in time, when consumers are really paying attention,” says Todd Pendleton, chief marketing officer of Samsung’s U.S.-based mobile division. “We wanted to take that opportunity and all that energy and make it Samsung’s moment.”
No doubt about it, Samsung is having a moment. In recent years the South Korean company has taken the mobile world — the U.S. included — by storm. Last year it overtook longtime leader Nokia to become the No. 1 player in cellphones, with 29% market share worldwide. In smartphones, those high-end devices with advanced computing power, Samsung is also No. 1 globally and in a dead heat with Apple in the U.S.: Most analysts show Apple with a slight edge in smartphone sales, while one outfit, ABI Research, says Samsung’s share of smartphone shipments topped 33%, compared with Apple’s 30%. (To be sure, Apple sells one device, the iPhone, while Samsung offers 25 unique smartphones in the U.S.) “Samsung is on fire,” says John Legere, CEO of mobile operator T-Mobile USA.
Chalk up Samsung’s success to a combination of marketing swagger, innovation, operational prowess, and a marketplace hungry for an alternative to the iPhone. Although Samsung wasn’t the first to develop a phone that runs on Google’s Android operating system, it quickly moved ahead of the pack by introducing one with a strikingly thin, bright, and large screen, and by rapidly rolling out cutting-edge features like the ability to “beam” photos by pressing together the backs of two phones. Thanks to tight control over an extensive supply chain (Samsung makes everything from screens to memory chips), it’s been able to move quickly to meet the rising demand for its mobile devices, churning out more than 215 million smartphones globally last year. And phone companies are so eager to stock Samsung devices that they’ve abandoned their practice of demanding exclusive deals on new phones; last summer Verizon Wireless (VZ), T-Mobile, Sprint (S), and AT&T (T) agreed to launch the Galaxy S III phone simultaneously — a major coup for Samsung.
Of course, not everyone loves the new Samsung. Apple has sued the company for patent infringement, and the phonemakers will probably be embroiled in litigation for years to come. And while Samsung has done a phenomenal job of building itself into a cool brand in a short time, it doesn’t wield much control over the wireless ecosystem — the mobile operating system, application store, and other software services that have helped make smartphones so popular. Indeed, some of the same forces that contributed to Samsung’s growth — the Android platform and app catalogue, consumers’ desire for the next shiny new toy — also leave the handset maker vulnerable to a raft of Android-based rivals, all gunning for the new No. 1. And don’t expect Apple to rely solely on the courts to fend off Samsung. Says T-Mobile’s Legere of the South Korean juggernaut: “I think they got the other guy’s attention.”
Samsung Electronics, No. 20 on last year’s Fortune Global 500 ranking, with $149 billion in revenue, has humble beginnings. Samsung, which means “three stars” in Korean, started out as a small supplier of dried fish and noodles in the city of Daegu back in 1938. Eventually the company’s ambitious founder, Byung-Chull Lee, moved the company headquarters to the country’s capital, Seoul, and expanded into new businesses.
In the late 1960s Samsung officially entered the electronics business. In the early years the company was known for cheap televisions and air conditioners. That all changed in 1995, when its chairman (and the elder Lee’s son), Kun-Hee Lee, paid a momentous visit to the company’s plant in Gumi, a factory town in south-central Korea. Legend has it that the younger Lee had sent out the company’s newest mobile phones as New Year’s presents and was horrified when word came back that they didn’t work. Later, at Gumi, he made a giant heap of the factory’s entire inventory and had it set on fire.
After the incineration at Gumi, spending on R&D increased, and Samsung started churning out top-notch products, like the world’s first MP3 phone, the highest-megapixel camera phones, and other high-end devices that could run on South Korea’s superfast cellular networks. But much of the world, especially the U.S., didn’t associate the Samsung brand with mobile, in part because the company let the telcos take the lead in marketing the devices.
By 2010, some three years after the launch of the iPhone, Samsung decided that its low-key approach wasn’t working, especially in the U.S. Dale Sohn, president of Samsung’s U.S. mobile operations, assembled his local leadership team to figure out a way for Samsung to control its own destiny, instead of relying on partners to tell its story to consumers. Sohn says he is in constant communication with his bosses in Seoul but also has a degree of independence to do what’s best in his home market. As a result, he adds, it wasn’t hard getting headquarters onboard with his plan, which later became known internally as the “paradigm shift.”
In June 2011, Sohn hired Pendleton, the former global brand communications director at Nike (NKE). By then Samsung had already launched its second-generation Galaxy smartphone, the S II. The 4 1/3-inch device came with built-in near-field communication capabilities and a cool function that mutes incoming calls when the phone is placed face-down. “We had a product that was better that was already in the market, but nobody knew about it,” says Pendleton.
Best Buy (BBY) or a phone-company store. Another exec, Brian Wallace, was brought in to handle digital marketing efforts. Wallace, in turn, brought in a data-analytics company called Networked Insights to help Samsung tap into and utilize the conversations across social media, a key part of its strategy to connect better with consumers. (In December Wallace said he would leave Samsung for a marketing gig at Google’s Motorola unit; Networked Insights is still working with Samsung.)Pendleton moved fast (the longtime Nike exec has a collection of some 600 pairs of sneakers). In just a year and a half he put together an entire marketing team from scratch. Ketrina Dunagan, his new VP of retail and channel marketing, opened Galaxy Studios — facilities where consumers can test Samsung phones instead of going to
Just a few months into the job, Pendleton also enlisted 72andSunny, an ad agency owned by Toronto-based MDC Partners. “At that point the main guys were Apple, and everyone else was fighting for the No. 2 spot,” says John Boiler, 72andSunny’s co-founder and CEO.
Boiler had worked with Pendleton on several Nike campaigns. It was his team that came up with the now-famous fanboys campaign, a series of ads that poke fun at diehard Apple fans. Over the past year 72andSunny has worked with Samsung on ads for four different products, including the Galaxy S III. In the most popular of the anti-Apple commercials — the one that aired during the iPhone 5 launch, it turns out that one of the hipsters waiting in line for an Apple phone is actually holding a spot for his parents. Ouch.
All that buzz doesn’t come cheap. Samsung spent $349 million on marketing in the U.S. in the first three quarters of 2012, compared with $191 million a year earlier, according to Kantar Media, a research firm. But CMO Pendleton is quick to point out that without a great product, all those dollars wouldn’t have much effect. Samsung spent $8.7 billion on R&D efforts in 2011. One in four of the company’s 220,000 employees works in research and development. Much of the phone technology is developed and produced by groups in Asia, then tweaked and packaged locally. Researchers are currently experimenting with innovations like bendable screens and new memory technologies — all of which are expected to be incorporated in future versions of its smartphones.
Indeed, part of Samsung’s secret sauce is that it controls and manufactures many of the building blocks of its phones. It has capacity to ramp up production of those parts quickly, which also makes Samsung a favorite among other phonemakers. One of its largest components customers? Apple. “All of their competitors must use third parties to accomplish the same tasks,” says Len Jelinek, a semiconductor analyst at research firm IHS iSuppli. “One could estimate that there would be at least a quarter’s advantage due to internal control of all operations.”
Samsung’s relationship with Google (GOOG), maker of the Android operating system, has also evolved. Samsung launched its first Android smartphone, the Galaxy S, in 2010, well after HTC came out with the first so-called Google phone. Once Samsung embraced Android, though, it became the platform’s No. 1 performer: Today it makes 45% of all Android-based phones. Samsung also collaborates with Google on chip technology, says Andy Rubin, senior vice president of mobile at Google. “We worked together on several Nexus products, and the partnership has also prepared the platform to take advantage of the advances in embedded processors,” Rubin writes in an e-mail.
Samsung’s reliance on Android unquestionably accelerated its growth in handset sales by offering it a “turnkey” mobile ecosystem. But Android could also turn out to be its Achilles’ heel. While it builds some services on top of the operating system and tries to give its Galaxy phones their own look and feel, Samsung ultimately does not own Android. In fact, the operating system is freely available to all other phonemakers, including up-and-coming Chinese manufacturers that are developing cheaper phones. Then there’s the fact that Android’s parent, Google, now owns Motorola Mobility. It remains to be seen if Samsung will enjoy the same friendly partnership with Android if Google decides it wants Motorola to grab market share.
Samsung claims that being “open” gives it the flexibility to shift gears if a particular operating system falls out of favor. The company has already announced a Windows Phone 8 device, the Ativ Odyssey, which will launch in the U.S. in the coming weeks. It also said it will make a phone that runs on Tizen — an open-source operating system backed by Intel (INTC) — later this year.
“We don’t own the ecosystem because we’ve chosen up to this point not to innovate in that direction,” says Justin Denison, VP of strategy and market intelligence at Samsung. “Where we choose to innovate is creating the best package possible for the consumer.”
Still, Samsung is working hard to build up its own content and services on top of Android, like its Music Hub offering, which allows users to purchase and download songs or store them in the cloud for streaming. To help beef up its software know-how, the company is expanding its footprint in Silicon Valley. In December, Samsung announced it would soon open a new startup incubator in Palo Alto. The company is also building out a 1.1-million-square-foot R&D center in San Jose.
Samsung’s executives won’t say if they intend to develop their own operating system. Industry observers say that without full control over all the pieces — hardware and software — Samsung could be missing out on a huge opportunity: getting all its consumer-electronics products to work together seamlessly. With a proprietary operating system, Samsung could enable its TVs to talk to Samsung-made phones and even washing machines. Applications and content could easily be shared among the different devices, making Samsung’s entire line of consumer electronics much, much stickier with consumers.
But gadget makers and Internet companies have been talking up such convergence since the 1990s, and many analysts aren’t holding their breath. “I don’t yet see that they’re moving into the next phase,” says Asymco analyst and Apple commentator Horace Dediu.
For now the Samsung U.S. team remains focused on developing and marketing the next hot device. While Sohn, the U.S. mobile president, and marketing chief Pendleton are pleased with the positive reviews and cool factor the Galaxy devices are enjoying in the marketplace, they recognize that success can be fleeting in consumer electronics. If Samsung doesn’t keep innovating and creating experiences that customers love, it may find itself on the outs — and maybe even the subject of a cheeky ad campaign. “Thanks for holding our spot at the Samsung store”? Ouch.
This story is from the February 4, 2013 issue of Fortune.