Hey, great question. I've dealt with this myself in a busy urban setting, and here's what I've found works best. The trickiest part with large trash cans—especially those 30-gallon or bigger metal or plastic ones—is that the liner tends to slip down when you toss in anything heavy. The best way I've found is to use a combination of a simple bungee cord and a liner with reinforced handles.
First, choose a heavy-duty liner (like a 3-mil or thicker bag) that has strong handles or a drawstring. Then, before you put it in the can, hook the bungee cord around the can's rim. Stretch it across the opening and hook the other side. Now, when you insert the liner, fold the top edge of the bag over the bungee cord so the cord holds the bag in place. This prevents the bag from slipping down when you add trash.
If you don't have a bungee cord, another solid option is using a liner lock ring—a simple plastic ring that snaps onto the rim and sandwiches the bag. You can find these at hardware stores. For a no-cost DIY hack, just use a few large binder clips to clip the bag's top edge to the can's rim from a few points around the circumference.
In my experience, the bungee cord method is the most reliable because it distributes tension evenly and doesn't require buying extra hardware if you already have cords. Just make sure the cord is tight enough. Also, avoid overfilling the can—when the liner gets too heavy, it will try to detach. Overall, this approach has kept my liner secure even in windy city conditions. Hope this helps you out.