I am a Canadian who has been living in Brazil for over 9 years and married to a Brazilian woman.
The easiest way to come to Brazil is on a tourist visa, valid for 3 months and is extendable for an additional three months at the Federal Police headquarters nearest where you will be living.
First of all it will be necessary to get all the paperwork and documentation you will need in order to get married ready before you come to Brazil, that will make things a whole lot easier.
You will need a certified copy of your Birth Certificate, the document must show the names and origin of both of your parents, your date and place of birth. This must then be translated into Portuguese by a legal translator in Brazil or the Consulate. You also need certified copies of all your travel documents, passport, visa, etc. This you can do at any cartório (registry) once here in Brazil. If you are divorced you must also provide a certified copy of your Certificate of Divorce, also translated.
Your Brazilian partner must produce all of his/her documents Registro Geral, CPF, etc. then the Cartório das Pessoas Naturais will set the date for a civil marriage.
Note that ALL documents and translations must be current, NOTHING issued any more than six months before the documents are delivered to the cartório will be accepted. Also you may want to have your Brazilian (Portuguese speaking) partner check the regulations at the cartório where you intend to marry. Regulations seem to vary from state to state. I got married in São Paulo, for example, because the document requirements were simpler. You may a wish to arrange a local lawyer to handle all the details at the cartório as well, generally the public service workers are much easier to deal with when it's a transaction with someone known to them.
As long as you are in the country legally, all travel documents up to date and have all the necessae ry documents in order... it is your legal right to get married here in Brazil, foreigner or not.
Once you are married then you present yourself to the Federal Police, produce the documents they require and a Certified Criminal Record check from your country of origin (you must not have any convictions) and apply for your permanent visa. Be prepared for a very long wait for the process to be completed, sometimes years.
A civil marriage gives you the right to remain in the country legally while the process is taking place. But you have to get all of this paperwork done and enter the permancy process within the 6 month validity of your stay on an extended tourist visa.
Another thing to be aware of is the Criminal Record Check in your home country, that can take a long time to process so you might want to start that before you come to Brazil. The document "Atestado de Antecedentes Criminis" must then be legalized by the Brazilian Consulate in your country of origin while still current. Once legalized, that document does not lose its validity, it does not fall under the six month rule any longer.
The process, whether married or not, takes forever! Should you have a child with your Brazilian partner before the permancy process is concluded, re-apply immediately for a permanent visa based on having a Brazilian child. It is almost automatic.