I just don't get were they really so naive to think that they are capable to win against mercenaries and soldiers lead by king in an open field? Walls would give them advantage. Why they met him near the gates?
I'm assuming this description of events is correct https://www.youtube.com/watch?v=MoP9N5d4TiM as Wikipedia article too.
The video is largely correct yes following contemporary understanding of the events, and I'm particularly pleased it mentions the number of previous battles that tend to get ignored by earlier history.
The peasants couldn't "remain" on any walls because the peasants had no walls to remain on. They were not so naive to think they have good chances to fight in an open battle, but they also were desperate and had no other option. The peasants absolutely understand that being in a city would be a great advantage, that is most likely precisely why they were fighting outside the gates. The problem was that unlike how we look upon it today with an island and it's central town Visby mediaeval Gotland were 2 worlds: the Hanseatic city and the countryside. And these two were, literally, divided by the citywalls. While older views were of a deeper trade-competition based animosity between city and countryside have been softened by newer research the fact remains that the city and countryside fought a civil war in the late 1200s that resulted in the Swedish monarchy getting a more solid grip over the previously effectively independent island though it had been considered to lie in the "Swedish sphere of influence" and under the protection of the Swedish kings. The city effectively gained the ability to tax the peasantry's trade through the city at the cost of a fine for having illegally warred and built a city wall without royal permission. Gotland had been broadly speaking a more equally rich place, many silvertreasures have been found all over Gotland from the Scandinavian iron age. But going into the 1300s we have a Hanseatic city with a surrounding countryside it is to some degree living off (though I don't want to overstate this gulf).
So come the invasion. The peasants likely expected to be let into Visby to bolster the defence against the invasion which is why they were there. The problem is that the burghers decided that their interests did not necessarily align with the peasants. One interpretation is that they callously and cowardly decided to hang them out to dry breaking a supposed defensive pact. If we want to somewhat contextualise this, the peasants had already been defeated and suffered severe losses against the Danish. There is some evidence that the remaining peasant forces were of somewhat low quality, both equipment-wise and also in personnel. That is to say the more ablebodied men had already fought and lost. There is also the question of supply. The city simply had no stores yet to survive a protracted siege, especially not with extra mouths to feed as the harvest had not been brought in yet. So not only did the city not permit the peasants to enter they stayed passive during the battle too, likely calculating that they would have a better negotiating position of not having fought and facing a more exhausted enemy. Basically, there was little to gain for the city and much to lose by participating. As the city were able to negotiate accords with the Danish king that retained their old laws and privileges for a peaceful submission, that can argued to have been a correct but cold calculation. The peasantry could look forwards to being massacred as a force or when the countryside were ravaged so they chose to make a stand, ill-advised as it turned out, but then they likely expected the city to support them.
It should be noted that we don't really have too much information of what happened. There's a legend about the "fire taxation" (the Swedish term "brandskattning" is a lot more evocative) i.e. ransoming the city to avoid it being sacked, but there is scant evidence for it having happened. Though likely some kind of economic compensation occurred as there are letters to Lübeck suggesting payment was made for the Hanseatic goods in Visby. But we should remember the people responsible had a vested interest in making their plight seem greater than it was to excuse not having supported the rest of Gotland in the face of fairly likely existing defensive agreement.
So in short, no they were not naive. They were desperate and were likely expecting the city to admit them or join them in the battle, neither of which happened. Most likely because the burghers made the likely correct assessment that further resistance was futile. At that point it would have been too late to back down.