Some earbuds come with cases that can help prevent tangles, but many don’t. If yours didn’t (or if you lost it), a wine cork can do the job with just a couple of holes.
As one Instructables user explains, you can poke a small hole in the top of a wine cork to place the jack, and use a knife to cut a slot along the other end to place your earbuds. You can then wrap the length of the cord around the cork. This should help make sure that your headphones don’t end up a tangled mess when you toss them in your bag or purse.
Corkwrap | Instructables via Reddit