Is it permissible to buy
places where wine was drunk or there was dancing and to use them as mosques and places of
worship?
Yes, it is permissible to buy them and make them into mosques, because by doing so you are using them for a better purpose than they were used for previously. The evil is not part of the nature of the place itself, it is just something that happened there because of the purpose for which the place was used. Once it is put to a good use, the evil disappears and it becomes a good place.