An emotional Gates said: "I'm deeply honored and moved by your presentation of this award. It is a big surprise. But we should have known a couple of months ago; you're getting pretty good at this covert ops stuff. "
Gates appeared to be making a reference to the Navy SEALs raid in Pakistan in early May that killed al-Qaida founder Osama bin Laden.
A former CIA chief who spent 26 years at the agency, Gates was appointed defense secretary by President George W. Bush in 2006, but stayed on into the Obama administration at the president's request.
Gates steps down this week, to be succeeded by CIA Director Leon Panetta. At the secretary's departure ceremony at the Pentagon, Obama drew laughter while talking about the low-key Gates.
"You see, if you look past all of Bob's flashiness and bravado -- and his sharp attire, his love for the Washington limelight -- then what you see is a man that I've come to know and respect," the president said. "A humble American patriot, a man of common sense and decency -- quite simply, one of our nation's finest public servants."
The citation on the Medal of Freedom said in part: "Our Nation's 22nd secretary of defense, Robert M. Gates has selflessly dedicated his life to ensuring the security of the American people. He has served eight presidents, of both parties, with unwavering patriotism."
Gates thanked both presidents he served.
"The transition from the Bush to the Obama administration was the first of its kind from one political party to another during war in nearly 40 years," he said. "The collegiality, thoroughness and professionalism of the Bush-Obama transition were of great benefit to the country, and were a tribute to the character and judgment of both presidents. ...
"Shortly I'll walk out of my office in the E ring for the last time as defense secretary," he added later. The office "will still have looming over my desk the portraits of two of my heroes and role models -- Gens. Dwight D. Eisenhower and George C. Marshall.
"It is from Marshall that I take a closing thought, first delivered more than six decades ago in the opening years of the Cold War," he said. "Addressing new university graduates, Marshall extolled what he considered the great 'musts' of that generation. They were, he said, 'the development of a sense of responsibility for world order and security, the development of a sense of the overwhelming importance of the country's acts and failures to act.'
"Now, as when Marshall first uttered those words, a sense of America's exceptional global responsibilities and the importance of what we do or do not do remain the great 'musts' of this dangerous new century."
Benedict Cumberbatch's dramatic reading of R. Kelly lyrics is just what you need
Beyonce flaunts bikini body, Blue Ivy in vacation pics