Almost everyone needs a visa, which costs between US$20 and US$50, depending on nationality, for a single-entry visa valid for up to three months. It’s best to get the visa in advance (and necessary if you want multiple entry), though visas are currently readily issued at Dar es Salaam and Kilimanjaro airports and at most border crossings (all nationalities US$50, US dollars cash only, single-entry only). Some embassies require you to show proof of an onward ticket before they’ll issue a visa, though a flight itinerary will usually suffice.
One month is the normal visa validity and three months the maximum. For extensions within the three-month limit, there are immigration offices in all major towns; the process is free and straightforward. Extensions after three months are difficult – you’ll usually need to leave the country and apply for a new visa.