Humans have a strong built-in tendency to work together in groups. However this social niceness has a mirror image on the dark side: one of the things that they are especially prone to working on is hurting people who are not in the group. Sociologists call this "In-group vs Out-group". Its also known as "Us vs Them".
In a modern nation there are lots of potential groups, and so there is a centrifugal tendency. People form smaller groups around all sorts of things: places, ethnic identities, sports teams, political parties and the like. This leads to disunity in the country and opposition to its leadership.
The solution for the leadership is obvious: find a big threatening out-group somewhere outside the country and make it look like a threat. Talk about how "They" are a threat to "Us" (meaning the people of the country), and how we must all work together to defeat this threat.
This technique is well known, but that doesn't make it any less effective. A classic example was the Falklands War:
When Argentina’s military junta invaded the Falkland Islands, a British colony, in April 1982, Margaret Thatcher’s political future was in serious question.
Britain's first female prime minister was facing sharp criticism from both her cabinet and the public in response to her domestic policies. Savage government spending cuts, a declining manufacturing industry and high unemployment all pointed to an early exit for the leader.
Her quick response to the South Atlantic conflict and swift victory led to a surge in her popularity and subsequent reelection in 1983. She would go on to serve until 1990, making her Britain’s longest-serving prime minister of the 20th century.
(Side note: the extent to which the war was an election strategy rather than a principled response to a foreign invasion is a controversial issue. As it is a question of individual motivation it is off topic here unless some specific evidence (e.g. a memo by Mrs T. on the subject) comes to light).
Of course this all assumes that you win the war. If Britain had lost the Falklands there is little doubt that Mrs Thatcher would not have remained Prime Minister.
Another example has been visible in Iran in recent days. After the assassination of General Soleimani there was a huge surge of patriotism and support for the Government, which eagerly talked up the threat from the USA and the need to fight back.
Mirroring fierce threats of retaliation from across Iran’s leadership since Friday’s assassination, Salami threatened to “set ablaze” American interests in the region, drawing cries of “death to Israel” from the crowd.
However the government's credibility as leaders in a war against the USA was shredded when they were forced to admit (after three days of denial) that they had shot down a civilian airliner.
Just days before the flight crashed, Iran displayed an unprecedented level of unity and popular support when millions of people poured on to the streets all over the country to mourn the death of Soleimani.
This seemed to indicate that, when faced with the external threat of military confrontation, Iranians from different political and economic backgrounds could come together and put aside their divisions.
But the shooting down of flight PS752 and the subsequent denials from the authorities could lead these divisions to re-emerge and become even sharper.
This is likely to revive the divisions and unrest that erupted in November when the Iranian government approved a sharp spike in fuel prices. This move triggered large demonstrations across the country and resulted in widespread repression and the killing of at least 300 people.
Trump knows all this just as well as anyone else; the quoted tweets accused Obama of using this as an election tactic. Of course, with the next election looming ahead he might find it advantageous to play the same game himself.