Gabigol (2), Gabriel Jesus and Luan all found the net in the match in Salvador. Brazil will now face Colombia.
Brazil's footballing men finally found their scoring touch at the Olympic tournament, defeating Denmark 4-0 to advance to the quarter-finals and avoid an embarrassing elimination in front of the home fans.
The hosts will play Colombia on Saturday in São Paulo, a mouthwatering showdown between the two South American neighbours.
Brazil had been held to scoreless draws in their first two group games and needed a victory to guarantee a spot in the knockout round.
The host side finished first in Group A with five points, one more than Denmark, which also advanced and will play Nigeria.
South Africa and Iraq were both eliminated after a 1-1 draw.