In August 22nd 1942 Brazil officially declared war on Germany and Italy. The official reason was the sinking of 5 Brazilian ships by German submarines between August 5th and August 17th, with the deaths of many civilians and military. Spontaneous protests happened in several Brazilian cities against the Axis and the Brazilian government then declared formally war on the Axis countries.
Why formally? Because Brazil was helping the Allies (USA and the UK) with material and military bases (in the Northeast region of the country, which helped US operations in the North African theatre). So that was the reason the submarines attacked Brazilian ships. But why was Brazil so ambivalent to declaring war on the Axis?
The Brazilian political scenario in the 1930s and 1940s was complex. Brazil had become a dictatorship in 1937 as Getulio Vargas, who had taken power in a revolution in 1930 and was re-elected in 1934, made a coup d’etat, creating the “Estado Novo” (New State), that would last until 1945. There are several reasons for the coup, but the Communist attempted coup in 1936 (intentona comunista) was one of the causes. It must be stressed that Vargas closed all political parties in Brazil, including the most fascist one (Integralistas), expelling various integralists from the country and political or military offices. Vargas was a dictatorship more along the lines of caudillos of Latin America rather than fascist. One of the intellectuals who supported Vargas, Oliveira Vianna, said that he didn’t have a party, like fascist or nazist states, that his party was “the president”. Of course, it must be said that it still was a dictatorship, with press censorship, propaganda, leadership cult, etc, just that it didn’t fit the fascist mold completely. Eventually the fact that Brazil was a dictatorship fighting “for democracy” with the Allies would result in the “Estado Novo” ending in 1945 and democracy returning, but that would take a long time.
So, to actually answer your question, Brazil in the 1930s and 1940s knew that the geopolitical scenario in the world was very dangerous and in a flux. The top government officials knew that war was coming in Europe and possibly the world (and, more worryingly, maybe with Argentina) and the top military brass wanted to update the country’s military equipment. The civilian government officers also wanted foreign help to industrialize the country. So foreign policy in the end was guided by who could help Brazil achieve these objectives (armaments and economic help).
The Navy favored equipment that came from the British Royal Navy, while the Army was divided between equipment from the USA and from Germany. Some very important military officers (Gaspar Dutra and Goes Monteiro, chief of the army and joint chief of staff, respectively, for example) favored Germany, as they saw the country’s military successes. But then there was the question of how to actually get that equipment. As the war started, the British navy blockaded Germany, which led to a very big decrease in foreign trade between Brazil and Germany. It went down like 90% in one year. Germany already wasn’t using hard currency to pay its bills, so this was also a negative factor in getting help from Germany in the armament business.
The USA courted Brazil, and other Latin American countries, as dominating the Western Hemisphere and not allowing it to have an Axis presence was seen as very important in the war strategy. There was a lot of negotiation going on in 1941 and 1942, but eventually the US government accepted to give to Brazil most of what it wanted: military equipment to update Brazil’s armed forces and economic help in improving the country’s infrastructure and industrializing the country. That’s the main reason why Brazil sided with the US.
Anyway, there are reasons why the US took so long to seal the deal. The military was reticent in giving military equipment, like tanks, to Brazil first while the US military still needed it. Also the State Department didnn’t like putting effort in industrializing Latin American countries, because it saw these efforts (mostly already started by the LatAm countries) as creating artificial industries that wouldn’t sustain themselves, deviating resources that could be better spent elsewhere in the war effort and that could, if successful, result in less markets for the US companies in the future.
Brazil exchanged this help from the USA for letting the Allied forces use Brazilian airbases and ports in the Northeast of the country. Also Brazil was the source of many strategic resources, which the US soon tried to monopolize, like rubber. The US also demanded other stuff from Brazil, like monitoring the activity of population that came from the Axis, which results in several anti-Axis measures, like seizing Japanese Brazilian property and expelling Japanese Brazilians from the coast.
The Axis submarine attacks happen in this context, of Brazil helping the Allies without declaring war on the Axis. The government officials, seeing the popular reaction to the deaths of the Brazilians and noticing that they could get even more help if they formally were a part of the Allies, then decided to go full into the war. If Brazil remained neutral it was going to be attacked by the Axis submarines and wouldn’t get as much help as in the end it got.
Source: Relações exteriores do Brasil, 1939-1950: Mudanças na natureza das relações Brasil-Estados Unidos durante e após a Segunda Guerra Mundial; Ministério das Relações Exteriores do Brasil
r/http://funag.gov.br/loja/download/998-Relacoes_Exteriores_do_Brasil.pdf