I don't sell packaged cookies at farmers' markets, but I do sell my books at them, and they've, surprisingly, been a great venue for books. I have some tips that may apply more broadly:
1. Try to get into the markets (book a spot) before the season starts. Some can be tough to penetrate once the market opens for the season and vendors are already locked into spots.
2. Some markets charge you to have a booth. Be sure to factor these costs and also the time to transport, set up, and man the booth into the price of what you're selling in order to make a financial return on the event. The transport and setup for these things can be substantial.
3. Do some free sampling, but nothing elaborately decorated. Just a nibble to give people a sense of your product taste and quality. Samples will draw people to the table, but if you give away too much (or things that are too highly decorated), you'll get a lot of grazers (no buyers) and probably not make much money.
4. Invest in a Square card reader or equivalent. It's astounding how many people come to markets without cash and, though Square takes a cut of all purchases, I've found that having more payment options has always increased sales more than enough to make up for these costs.
5. Bring someone with you, ideally someone willing to volunteer their time. This may sound like a no-brainer, but the tendency when starting a business (at least for me) is to try to go it alone to save on expenses. But it's really hard to field questions, handle product, handle the transactions, and be super pleasant to everyone all at the same time.
6. Bring enough change - again, this may seem like a no-brainer. But people bring more $20 bills than anything else to markets.