Gotham is a name that has meant farmyard, village idiot, New Yorker and superhero metropolis, in that order.

Start with the village. Etymonline says a Nottinghamshire village recorded as Gatham in 1086 has an Old English name meaning an enclosure where goats are kept. It is not known whether that is the Gotham of the stories.

Those stories are medieval jokes about the "Wise Men of Gotham." Wordhistories.net places the earliest occurrence in the mid-15th-century Towneley plays, with a jest-book, A C Mery Talys, following in 1526. Wikipedia lists the typical tales: villagers trying to drown an eel, hedging in a cuckoo, and rolling cheeses downhill. A legend says they acted foolishly to stop King John building a road through their village. Wordhistories.net calls the village "probably fictional" and says it is uncertain whether the Nottinghamshire one is meant.

The second life began in New York. Washington Irving called the city Gotham in Salmagundi, a satirical periodical. Wordhistories.net says the name appeared in the issue of 4 February 1807, though it is often dated to November of that year. He meant it as a joke at the city's expense, and "Gothamite" for a New Yorker followed.

The third life is the superhero's. Wikipedia credits writer Bill Finger with naming Batman's city. He tried Civic City, Capital City and Coast City, then paged through the New York phone book and stopped at "Gotham Jewelers." Finger said he wanted readers in any city to identify with the setting, so he did not use New York. Gotham was first identified as Batman's home in Batman #4, dated December 1940.

The name therefore carries two layers of irony. A village of supposed fools became a nickname for America's biggest city, and then a stand-in for every city. Most readers who know it from the comics have never heard of the goats, or the cuckoo.