Because the country is so large, the "best time" depends on your destination:
This region has the most distinct contrast between wet and dry seasons.
To understand Brazil’s weather, you have to look at it through five distinct climatic regions.
Brazil is a massive country (the 5th largest in the world), spanning from 5°N to 33°S latitude. Therefore, Instead, the country contains six main climate subtypes: equatorial, tropical, semi-arid, highland tropical, subtropical, and coastal.
If you are traveling from North to South (e.g., starting in the Amazon and ending in Porto Alegre), pack layers. You might need swim trunks and sunscreen in the north, but you will need jeans, boots, and a heavy jacket if you head down south during their winter months.