President Obama surprised Defense Secretary Robert M. Gates on Thursday with the Presidential Medal of Freedom, paying tribute to his four decades of public service at a regal farewell ceremony outside the Pentagon. The honor came on Gates’s last day as defense secretary after four and a half years in the job.