It evolved because the lakes produced by dams are useful. It is not totally clear what they are useful for, but they must have been useful for the beavers who built them, not just any old beavers. The best guess seems to be that a lake provides a beaver with a safe place to build its lodge, out of reach for most predators, and a safe conduit for transporting food. Whatever the advantage it must be a substantial one, or beavers would not devote so much time and effort to building dams.