The ability to wiggle your ears comes from the auricular muscles. Once upon a time, these muscles allowed our ancestors to have directional control over their ears, like you see with dogs and many other mammals today. I've read that because our ancestors started living in groups, the need for this ability pretty much vanished and over time we lost this ability (seems like a reasonable theory to me). So while wiggling your ears no longer serves any evolutionary purpose, the muscles that allow you to do this are remnants from something that was an evolutionary advantage back before we lived in groups.
Wikipedia page (doesn't have much info but at least shows the structure of the muscles):