Zeus was the main god worshiped in Lystra, and the city had a temple for him. The people thought Barnabas was Zeus, likely because he seemed more impressive or powerful. They thought Paul was Hermes because he was the main speaker.
The people of Lystra believed the gods were visiting them again. This idea comes from a well-known story by the poet Ovid. In that story, Zeus and Hermes visited the area but were not recognized, except by an old couple (see Ovid, Metamorphoses 8.616–724). Because of this, the people tried to honor Paul and Barnabas as gods.