How do two UARTS know which baud rate to use?
Ordinary UARTs have to be pre-configured with the desired baud rate (as well as word length, stop bits, parity, etc) traditionally by a human.

For several decades now though there have been implementations of "auto baud" detection found in some settings, which typically works by timing key features of the waveform to deduce the baud rate.  Early versions needed a known character to be transmitted, but more sophisticated versions might be able to find the rate from more arbitrary data. 

A receiving UART typically has a local clock that runs at a faster rate - typically 8 or 16 times the baud rate.  This is used to sample the incoming signal and detect the bits within a word in a way that can tolerate a few percent of error.  Even two crystal oscillators wouldn't match rates perfectly, but the error tolerance can permit use of some less precise sources, sometimes including trimmed on-chip oscillators, etc.  It  can also help accommodate the fact that dividing down popular oscillator frequencies may only produce an inaccurate approximation to certain baud rates - in the old days, UART master clocks sometimes needed particular frequencies to access popular baud rates, for example 11.0592 MHz on the 8051 family.