There are two grammatical clues that help identify the antecedent -- gender and number. When those don't narrow the field to one candidate, you have to rely on semantics. Alas, grammar can take you only so far. Let's look at your examples:
The city of Conway, Wales, now surrounds the medieval walls and castle
that originally enclosed and protected it.
Gender doesn't help here as no persons are mentioned, but the pronoun is singular, so number eliminates walls. That leaves you with the city and the castle, but castle doesn't make sense: the castle wan't there to protect itself. It was there to protect the city. Now we can see that the author meant
The city of Conway, Wales now surrounds the medieval walls that originally enclosed the city and also surrounds the castle that originally protected the city.
True chalcedony is different from blue agate in the purity of its
pale-blue color and, of the two, (it is) the gemstone preferred by
Neither number nor gender helps here. We have four singular common nouns to match the third person singular it. We can eliminate purity and color, since they're not gemstones. So which is it, true chalcedony or blue agate? The sentence doesn't actually say which stone has the higher purity of color; all it says is that the two stones differ. We have to infer from the adjective true that chalcedony differs in a better way from agate. And, indeed, chalcedony is the more valuable gemstone.
The essayist writing on art in America was less concerned with why
funding was decreasing than with whether it was becoming less
popular with the public.
On gender grounds alone we can eliminate essayist as the antecedent. On semantic grounds, we can eliminate America. So what is the writer's greater concern -- the popularity of art or the popularity of public funding of art? Here you have an ambiguity not resolvable from the words. We might make a reasonable inference from the structure of the sentence that the essayist meant to stay parallel: "why funding was decreasing" balanced with "whether funding was less popular", but it's not dispositive.