I noticed that a lot of countries introducing quarantines and other measures to fight COVID-19 also included a ban on people gathering. I am wondering, how can it be enforced?
In normal situations, this would be hard to enforce. In this pandemic situation, however, a lot of things have changed.
The most important change is in one's own mindset. Many people will be aware of the gravity of the situation, mostly from seeing the situation abroad. For many Europeans, it started by seeing scenes from China, then South Korea, and eventually it hit closer to home in Italy. At the same time, the virus has gained ground in a number of states in the US. Combine that with the observation that the disease affects the elderly particularly hard and the fact that many people have elderly loved ones, many people can imagine how the disease may affect them. This means many people will be more willing to comply with measures they'd normally find outrages.
Another change comes from the top down. It starts with businesses that can move their operations to employees' homes, either to ensure their operations are less affected if an employee falls ill or because of recommendations by the government. That means it gets a little bit less busy on the roads, in public transport and in shops. Eventually, other businesses follow suit because they have less work, possibly because of problems in the supply line or lack of demand (e.g. travel-related businesses, non-essential shops, etc.).
Combine the two and daily life shifts from public places to people's homes quite a bit already. People have less reason to go out (they work from home, they are otherwise not needed at work, leisure activities one might normally go to are suspended, etc.).
Finally, the step to imposing more restrictive and compulsory isolation measures is much smaller than it would normally be. There will be much fewer people who go out to break these restrictive measures and there may be some social control in busier places as well.
When a government wants to stop people protests and similar actions the ban can be easily enforced, because protesters must put themselves in evidence otherwise their protest would have no purpose. But in the case of an epidemic the ban should prevent also people gathering in a discreet manner in a backstreet or a public garden. No country, not even the authoritarian ones have enough policemen to monitor every single corner.
Furthermore if people violate the ban and refuse to pay the fine, what are they going to do? Put them in a crowded prison? Wouldn't that defeat the purpose?
Enforcement can be tricky. You're right that not everyone can be controlled, but in the most troublesome situation they can be. Of course, one or a few people together is less of a problem that larger crowds. Larger crowds are often related to some event or business and that gathering could be disbanded. There could also be roads checks to prevent people without valid reasons from travelling longer distances.
So, it will not prevent a few people deciding to meet together in their own home, but it may prevent larger gatherings, especially in public places.
Last detail if there are limits in the way a ban can be enforced. How effective can a ban be at preventing public gatherings?
They can be very effective because for the reasons named it will prevent most people from going out unnecessarily when they normally would. It all comes down to leadership and getting citizens to see why it's needed so they'll comply willingly.
If this were done in Europe when the first cases were reported in China then this would probably backfire. Now that the public mindset in many places has evolved to understand imminent implications, something that will have affect very soon and close to home (as opposed to something that might happen in the future, somewhere), it will probably work as a social distancing tool.