Why are they raising interest rates in Brazil? How does raising interest rates in Brazil (and Russia) help fight inflation and defend their currency?

I understand that the basic model is that raising the rates is meant to encourage banks to lend in brazil to earn on the interest rate differential and it should also reduce local demand for goods and services.

But, both Brazil and Russia are facing recessions and their economies are currently weak. If you raise the interest rates to 14.5 like Brazil has done wont that make the economy even weaker which will make investors flee the country and thus weaken the currency which in turn will increase the price of all imported goods (which for most open economies is a big percentage) and thus make goods more expensive, thereby increasing inflation rather than reducing it.

I would have thought a better way to fight inflation in a recession would be to lower rates to stimulate the economy and thereby stimulate the stock market and attract foreign investment which would strengthen their currency and reduce the price of imported goods. What have I misunderstood?
Brazil raising interest rates probably refers to the central bank raising their interest rate which means that it is more costly for banks to loan money from the government and therefore the amount of money in circulation (as per quantity theory) should go down. It is a policy decision (possibly wrong depending on who you ask) in a difficult economic situation where a country faces both inflation and a downturn.

Your logic about the economy becoming even weaker might very well be true (and I'd agree with lowering rates being a better option). The questions about effects to trade are slightly more complex and not necessarily as straightforward.