Levees protect cities from flooding by acting as walls that prevent the river from overflowing its banks and inundating the surrounding areas.