Please check with the relevant Embassy in your country for up to date visa and passport information. Make sure your passport is not going to expire within six months of your trip (i.e. if your trip is in September make sure your passport is valid until at least March of the following year) and that it has at least 3 clear pages.

You can obtain your visa online https://eservices.immigration.go.tz/visa/, or at the point of entry (border / airport).
The time taken to clear immigration is roughly the same, regardless of whether you already have your visa. The cost of most tourist visas is U$D 50 per person and U$D 100 for American citizens, to be paid in cash.