One spectator at the rally was shot dead, while two others were critically injured.
The male attacker was later shot dead by the Secret Service, officials said.
The shooting has been condemned by Mr Trump’s main opponent in the presidential election, President Joe Biden, as well as former President Barack Obama.
Mr Trump later took to his Truth Social network to express how he felt during the shooting. He said a bullet pierced the “upper part” of his right ear.
The FBI identified the attacker as Thomas Matthew Crooks, a 20-year-old man from Pennsylvania. He was shot dead by police at the scene.
Crooks was a registered Republican, the Associated Press have reported.
In his reaction to the shooting, President Biden said, “There’s no place in America for this kind of violence. Everybody must condemn it.”
Mr Biden later spoke to Mr Trump, the White House said.
In his reaction, former President Obama wrote on X that there “is absolutely no place for political violence in our democracy.” He said he was “relieved that former President Trump wasn’t seriously hurt.”
It is unclear how the shooting will impact the November US presidential election that sees Mr Trump locked in a close race with Mr Biden of the Democratic Party.
Mr Trump, 78, is popular among conservatives and right-wing voters and has repeatedly campaigned against immigration and vowed to deport thousands of illegal immigrants.
Mr Biden, 81, who is currently the Democratic Party candidate, supports a more inclusive society and has created pathways for illegal immigrants without crime records to become American citizens. He faces internal criticism from his party following concerns about his cognitive ability, especially after a poor debate performance against Mr Trump. Although there have been calls from some Democrats that he should drop from the race, the president has said he would not and would defeat Mr Trump in the election.