Former President Luiz Inácio Lula da Silva has defeated far-right incumbent President Jair Bolsonaro in an extremely close race.