Obama said he believed in Nelson Mandela's vision of "equality and freedom and multiracial democracy", and that "a world governed by such principles is possible, and it can achieve more peace, and more cooperation in pursuit of a common good".
The "Mandela 100" anniversary has triggered a bout of memories and tributes to the late anti-apartheid leader, as well as a debate over his legacy and South Africa's fate since he stepped down in 1999.
He decried the brand of Trump's politics that has overtaken much of the Republican party, including "far-right protectionism and closed borders, but also on barely hidden racial nationalism". Many developing countries now are looking at China's model of authoritarian control combined with mercantilist capitalism as preferable to the messiness of democracy.
He said that there was a choice between two visions of humanity's future that the world must choose between. "Every generation has the opportunity to remake the world", Obama said. "To believe that recent shifts in global politics are too powerful to push back against".
"But we need to resist that cynicism. I am not being alarmist I am simply stating the facts", he said, adding that "strongmen politics" are suddenly ascending.
He warned that countries that engage in xenophobia "eventually ... find themselves consumed by civil war". He also criticized what he called the "utter loss of shame among political leaders, where they're caught in a lie and they just double down and lie some more".
'Putin's poodle': newspapers declare Trump a traitor after Helsinki summit
Trump did have one prominent defender: Senator Rand Paul, who vied with Trump for the 2016 Republican presidential nomination. On his way home, Trump insisted in a post on Twitter that he has "GREAT confidence in MY intelligence people".
There are probably more than a few on the left, however, who wish Mr Obama would give more than a few speeches and carefully worded statements.
"Given the odd and uncertain times we are in, where each day's news cycles bring more head-spinning and disturbing headlines, I thought maybe it would be useful to step back for a moment and get some perspective", he said.
"We are thrilled to share our vision of a 4-part miniseries of Madiba's journey with South African audiences as we celebrate his 100th birthday". If you start (by) saying it is an elaborate hoax. where do we start?
"We've to be careful to say somehow that these issues don't apply to us", he said to a rousing applause. "We've been in lower valleys", he continued.
When visiting his late father's homeland for the first time as USA president in July 2015, Mr Obama launched an unprecedented defence of gay rights in Africa, telling Mr Kenyatta that the state has no right to punish people because of "who they love".
Obama also made a point of visiting Mandela's prison cell, and gave a moving eulogy at Mandela's memorial service in 2013, saying the South African leader's life had inspired him.
Obama was cheered by thousands in Johannesburg's Wanderers Stadium as he marked the centenary of Nelson Mandela's birth by urging respect for human rights, the free press and other values he said were under threat.