We see in several series that the UT isn't always able to translate all items.
We also see the system is usually able to make correct deductions from a variety of units, but not always.
Let's look at how one comes to a translation for the UT...
Units constructed from body part labels exist in most Terran languages, and by a quick scan, similar units can be translated close enough for verbal approximation. If the unit of measure also triggers the brainwave pattern for the body part, odds are they're homonyms, if not totally interchangeable terms, such as feet, hands, or man-heights.
Likewise, for radio and television capable species, many common objects will have reference sizes, mentioned in transmissions. Especially tall buildings in travel shows. A televideo of "The Urqar Tower Experience" will show it, and likely give it's height overall. If it says the building is "245 shobliks tall" and it measures to 130m, then a shoblik is roughly 58.06cm. Large objects can be readily scanned from orbit and accurately remotely measured, and televideo shows are likely to provide accurate enough measures. Likewise, time increments will usually be consistent, and the time unit subdivisions figured.
Further, many units of time can be grabbed from the Radio and Televideo programming. Most Television on Earth runs in Hour long blocks, often subdivided into 15 minute or 30 minute blocks for shorter programs. News channels are likely to continuously mention the time in units of the second or third level subdivision, depending upon how large the major subdivision is. Further, most units of frequency will be in a tertiary or smaller subunit, and you can compare their cycles unit for conversion to Hertz - Cycles per second. If you can figure out the numbers, you can figure out the frequency time unit by seeing how many waves it takes on that frequency to generate their rating, and get a fairly precise timing increment. It's then just a matter of listening for units. (For reference, our subunit is the hour: 1/24 day; the Secondary is Minutes, and Tertiary is seconds. Not all Terran cultures use 1/24 of the day cycle - Several used 12. Most frequencies are assigned with 3-5 significant digits, and the remainder truncated.)
If the ship's lucky, there are children's programs airing, as well. In such cases, many conversions will be painfully clear.
For primitives, drop a drone unit near the village, with a silent scanner and a high gain set of directional mics, and track the sounds, reverse engineer the raw sounds, and in a few days, have a basic lexicon. If the drone also is able to pick up brainwaves in field effect, then so much the better - the sounds and brain waves will be more tightly linked.
So, we can see how a ship coming into a technically sophisticated system can glean quite a wealth of knowledge. We seldom see a lot of the "backstory" to the Universal Translator, except on Enterprise, where Ms. Sato is working on it. But there is another element...
... Several older starfaring races now long gone, and apparently who dealt with several worlds. The Iconians and the Old Kings of the Klingons, at least. Plus the Q. This means borrowed words and terms influencing local languages. Which makes for easier translation as well. Some of the more stable terms over time are body parts and measurement units. The Ynch changed spelling, but is still the same unit it was in Old English; the actual distance varies by up to 10%, but it's the same term. Drams, Scruples, Pounds and Ounces go back to Rome, their original non-measure meanings lost, but their measurement still being used.
The combination of scanning the frequencies on the inbound, massive computational power, and the best programmers and linguists from dozens of worlds, plus hundreds of lexicons, and common influences in the histories. The only thing amazing is the speed at which they can compile the data. And that's aided by arrival at warp providing squirt trasnmissions.
As for the primitives, we must presume the use of brainwave scanning at close range. But even then, that's far better than the best we have, and what we have can translate known written language in seconds per page.