Six out of seven judges of the Supreme Electoral Court in Brasilia ruled that Lula was not allowed to compete in October while one in a hearing voted late into the night.
Brazilian law bans candidates whose conviction has been confirmed
Lula da Silva, commonly known as Lula, is a founding member of Brazil's only socialist party, Partido dos Trabalhadores (PT), the Labor Party.
As a former metalworker and union leader, he rose through the political ranks between 2003 and 2011 to serve two terms as President. He left office with 90% approval after his policies helped lift millions of Brazilians out of poverty.
But he has been imprisoned since April after being sentenced to 12 years for corruption and money laundering. The allegations against him came after his departure from office in 2011.
The 72-year-old has denied any wrongdoing. His defense said he was a victim of political persecution.
The lawyers of Lula da Silva can appeal the final court decision.
The Workers' Party said in a statement: "We will present all appeals to the courts for the recognition of Lula's political rights, as envisaged by the law and the international treaties ratified by Brazil."
He was registered as a PT candidate at the upcoming vote in August.
He said in a statement on his official website that he had submitted his candidacy "with the assurance that I can do much to free Brazil from one of the worst crises in history."
He also accused political opponents of convicting him without proof to take him
His conviction is extensive Corruption investigation of state oil company Petrobras, "Operation Car Wash", back  Lula Da Silva was accused of benefiting from the renovation of a triplex in a beach town near Sao Paulo by the construction company OAS. The charges involved 3.7 million Reais bribes ($ 1.1 million) received by OAS through the beachfront apartment. In return, Lula da Silva helped the contractor to purchase contracts with the prosecutor.
Lula da Silva was initially found guilty in July 2017 of the prosecution.
He was first interviewed in March 2016 by the police on corruption allegations. Lula da Silva's wife, Marisa Leticia Lula da Silva, who died in February 2017, and six others were also charged.
Lula da Silva was born into a working class family, left school and started working at the age of 12. Dissatisfied with the lack of political representation of the working class in Brazil, he opted for politics and helped found the Workers' Party in 1980.
He was friends with the late Cuban leader Fidel Castro, who supported his political career and Castro's funeral visited in 2016.