The long line outside Philz Coffee in downtown Palo Alto is filled with hipsters and techies eager for the slowest made-to-order drip coffee in town. Most of them work for nearby tech companies like Palantir Technologies, SurveyMonkey, and MongoDB—some of the hottest startups in the area. It’s hard to tell by their uniform of T-shirts, hoodies, and jeans, but it’s likely that this group of twenty- and thirtysomethings contains a sample of the most sought-after young professionals in the country. Above them, to the right of the door, is a sign that says PHILZ COFFEE: NOW HIRING ROCK STAR TALENT.

Welcome to Silicon Valley, where employees (yes, that includes baristas at Philz) are not just employees—they’re rock stars. This is the land of luxurious perks (think massages, onsite haircuts, and free food) and high starting salaries. It’s also a place where entrepreneurial dreams take flight—there’s always the potential, however slim the odds, of getting rich quick by working for a startup with a successful “exit.” And it’s not just the money and benefits that attract the rock stars. Out here young people feel they can make an impact—a.k.a. change the way the world shops, banks, gets around, and accesses health care—one algorithm at a time, which many young people value more than job security. As venture capitalist Marc Andreessen recently wrote in his blog, “Silicon Valley isn’t just a place—it’s a state of mind.”

Silicon Valley companies compete tooth and nail for top talent, but they’ve had an advantage, since the Bay Area has become a mecca for some of the country’s best and brightest. For years, graduates of elite universities like Harvard, Yale, and Cornell who once dreamed of working on Wall Street have flocked to the Valley, a trend that is only growing. Case in point: The percentage of Harvard Business School graduates who opted for jobs in the tech sector rose from 12% in 2012 to 18% in 2013, while the number of students who decided to start a career in finance dropped from 35% to 27%. And while once-scrappy Google, one of the jewels in Silicon Valley’s crown, recently celebrated its 10-year anniversary as a public company, it remains a top choice among graduates. According to a recent survey from research firm Universum Global, engineering and business school students consistently rank Google as the company they most want to work for.

To compete, top banks like Goldman Sachs reportedly have upped their starting salaries and bonuses for interns. Other industries, like manufacturing and transportation, are also trying to find ways to attract young employees, especially as new skill sets like coding, creativity, and collaboration become increasingly important in a more global, digital, and fast-moving world. And that talent crunch only increases as more and more baby boomers exit the workforce. (According to the U.S. Census Bureau, by 2030 more than 20% of Americans are projected to be age 65 and over, compared with just 13% in 2010.) “CEOs are just doing the math,” says China Gorman, CEO of Great Place to Work, a San Francisco–based human resources consulting and research firm. “Baby boomers are leaving, and there aren’t enough people coming in to fill their jobs.”

What’s at stake? As market shifts and rapidly changing technologies transform businesses, companies that don’t have up-to-date, evolving skill sets will fall behind. Hiring and retaining younger employees are more important than ever. “Companies that are able to hire, develop, and motivate the best people will have an obvious advantage,” says Claudio Fernández-Aráoz, a senior adviser at global executive search firm Egon Zehnder.

Of course, attracting a new generation takes more than beanbags and Ping-Pong tables (not to mention stock options). It can require a deeper shift in culture, including a more transparent leadership team and a constant effort to communicate values to current and prospective employees. And that—in addition to robust internship programs and other recruiting tools—is something Silicon Valley companies have gotten right.

Ironically, while the dearth of skilled talent will get only worse, across-the-board unemployment remains relatively high. “The paradox is profound,” says a recent report on global talent from Oxford Economics, an economic forecasting firm. “On the one hand, 40 million workers in the industrialized world are unemployed…Yet executives and managers tasked with hiring new workers often say they are unable to find the right people with the proper skills to fill their vacancies.”

Silicon Valley is also feeling the talent crunch. Ask any technology CEO what his or her biggest challenge is, and the answer will probably be finding and hiring talent. (That’s at least partly why the latest “trend” in the Valley is doing more outreach to prospective female and minority employees, not to mention hiring developers without the usual credentials.) And yet there’s a lot to be learned from them. Because if there’s one thing the tech sector gets, it’s that people—more than any other resource—are a company’s most valuable asset.

The Global Talent Crunch Over the next decade, it is estimated that the growth in demand for college-educated talent will exceed the growth in supply for most developed world. India's educated workforce is growing 2% annually, while America's is shrinking 0.8% each year.The Global Talent Crunch Over the next decade, it is estimated that the growth in demand for college-educated talent will exceed the growth in supply for most of the developed world. India’s educated workforce is growing 2% annually, while America’s is shrinking 0.8% each year.Graphic Source: Oxford Economics

French-born Aurelia Setton is one of those rock-star employees you often hear about in Silicon Valley. She graduated from the HEC School of Management, one of the top business schools in Paris, back in 2002, and later made her way to the Bay Area. After a stint in the Napa Valley wine industry, Setton got into Stanford University’s MBA program, during which she landed a product-marketing internship at Apple.

“I did the internship there because I wanted to work for the top American technology company,” says Setton, 35. One day she bumped into then-CEO Steve Jobs in an elevator at the Apple campus in Cupertino. The commanding chief executive officer stood right by Setton, who knew she had about 10 seconds to say something before he stepped out.

“I told him I was an intern and would soon be going to China to work on the Beijing Olympics campaign,” says Setton (the year was 2007). “He turned around and said, ‘I can’t believe we’re sending interns to China.’”

Regardless of how Jobs might have felt about giving interns such responsibilities, in Setton’s case the experience was invaluable. She later went to work for a startup in the energy field, because, she says, “I found energy and the environment to be something I really believed in.” After a few years she switched to the networking site LinkedIn. “It was a company that had a strong mission and strong brand.”

After about a year and a half working as a global sales manager for LinkedIn, Setton made another career move, accepting an offer to become a senior director at enterprise-software maker Medallia, based in Palo Alto.

“It always starts with alignment with the vision,” Setton says of her career moves. “It’s about having an impact and changing the world. My changes were always driven by companies whose mission I felt socially aligned with.”

Setton’s career trajectory is a perfect example of several new phenomena in today’s workforce. Her flexibility to switch between jobs and industries might make some employers see her as disloyal (a common complaint about millennials), but that chameleon-like ability to change every few years also means she can adopt new skills as her employers adapt to a rapidly changing, increasingly technology-driven and global corporate world. (Jobs didn’t know it, but one of the reasons Apple sent her to Beijing is that Setton studied Mandarin.)

At LinkedIn, one of Setton’s former employers, the acknowledgment that employees won’t stay with the company forever starts before they even join and isn’t perceived as a negative. Kevin Scott, senior vice president of engineering at the company, based in Mountain View, asks an important question of every candidate he interviews: “What job do you want after you work at LinkedIn?”

“Part of the reason Silicon Valley companies are so successful is that they’re a recombination of people who have worked in multiple companies,” says Reid Hoffman, co-founder of LinkedIn and co-author of a new book called The Alliance: Managing Talent in the Networked Age.

“Historically, most companies don’t want to ask that question,” says Ben Casnocha, an entrepreneur who co-authored the book with Hoffman. “But today your best people are not going to be lifers.”

For Setton, the pace of change and acquiring new skills come naturally—both in her personal and professional world. So does the need to believe in what her employer of choice does in the world.

That is something many tech companies got early on, perhaps because they were started by young people with aspirations similar to those of the employees they’re hiring. Google’s idealistic corporate slogan has always been “Don’t be evil.” Facebook’s mission is to “make the world more open and connected.” Twitter’s? To give everyone the “power to create and share ideas and information instantly, without barriers.” Even Philz Coffee has a mission: “To better people’s day.”

A recent article by Ravin Jesuthasan, global head of talent management at consulting firm Towers Watson, advises insurance companies on new means of attracting skilled workers. “One way insurers can attract workers is by underscoring the positive role risk management plays in modern society—the good it can actually do for individuals, groups, companies, even countries,” writes Jesuthasan.

Having a mission that resonates not just with customers but with employees can go a long way toward attracting younger workers, and not just in technology or insurance. Whole Foods Market, for example, has also been able to attract top talent, not only because of its higher-than-average wages and stock options, but also because it has a compelling mission statement: “Our vision reaches beyond food retailing. In fact, our deepest purpose as an organization is helping support the health, well-being, and healing of both people—customers, team members, and business organizations in general—and the planet.”

Of course, while having an effectively communicated higher purpose is key to getting prospective employees’ attention, actually getting young workers in the door requires a bit more work.

Tech companies compete fiercely to recruit on college campuses and at job fairs. And increasingly, like Square, they are turning to high schoolers to fill the talent gap. Facebook now recruits teen interns before their freshman year of college, and LinkedIn, which has hired 300 interns this year alone, has opened its summer program to high school students. Airbnb, meanwhile, has brought in interns as young as 16.

In the Valley interns are usually expected to write code, come up with ideas, and produce work just as any other employee would. “Interns are very important—they really are our lifeblood,” says Miranda Kalinowski, head of global recruiting at Facebook. “They do actual work—things that help us become more open and connected.”

Some tech companies outside the Valley get the need to expand too, and are opening internship programs to military veterans. Charles Phillips, CEO of the New York–based enterprise-software maker Infor, plans to hire close to 100 interns this year and says some of those spots will be reserved for veterans. He also says he is open to recruiting developers and designers who don’t have college degrees. Phillips, a former Marine, says, “There are plenty of talented people who didn’t go to school.”

In fact, if or where candidates went to college isn’t necessarily indicative of how successful they will be within a company, says Vivienne Ming, VP of research and insight at Gild, a San Francisco–based startup that applies big-data tools to finding and placing developers. An increasing number of tech companies now sell “people analytics” tools for use in hiring.

Expanding the pool of candidates also means accommodating employees who would have normally opted for working independently or outside the office. That’s why Santa Clara–based Intel, an older tech company, is experimenting with different work arrangements for entrepreneurial employees or those who want to spend more time with family.

“The workforce of today is very different from the workforce of a decade ago,” says Zahir Ladhani, VP of IBM’s Smarter Workforce division, which makes HR analytics tools. “Employees expect greater flexibility.”

Outreach and hiring are challenges, but so is retention. That’s partly why, once an intern—or any employee—has been hired, many Silicon Valley companies require weeks-long training. Facebook, whose engineering boot camp is a six-week program for new hires to learn its code base and culture, has created boot camps for other types of employees—from data scientists to sales and marketing. Apple University, set up in 2008 by Jobs, indoctrinates new employees to Apple’s approach to product design and other cultural elements. It’s also a way for Apple to integrate new employees after acquisitions, like the recent $3 billion purchase of headphone company Beats.

Not every company will be able to emulate Google—or Facebook or Square or Twitter, for that matter. Nor should it. But CEOs who are able to come up with their own twist on “Googleyness”—whether through internships, training programs, flexible hiring processes, or transparency with employees—will probably have an easier time attracting rock stars.

This story is from the September 22, 2014 issue of Fortune.