In their native tongue, most terrestrial sentient races have some version of the saying, "Good fences make good neighbors." Walls represent an evolution of this timeless bit of wisdom.
Built around settlements since antiquity, the earliest walls were fashioned from whatever native material was cheap, plentiful and easy to modify. Though there is no tradition of wall-building among those races whose natives do not live on the ground of their homeworld — Gand clouds or Toydarian swamp mats, for instance, cannot be walled off — when colonizing terrestrial terrain, these races tend to adopt the structure as well. The benefits are obvious, and fall into two categories.
First, walls aid in city planning. Enclosing a city with walls creates a rigid barrier between the city and the country or, metaphorically, between civilization and wilderness. Walls define a city's shape to passing ships and travelers. By setting gates at strategic points in the perimeter, a government can control the traffic into and out of a city, channeling commerce and trade as it pleases. On a more menacing note, gates can also be shut during times of civil strife to hold a population inside.
Second, walls help defend settlements in a dangerous galaxy. Walls help protect civilians and can slow an attacking ground force. This is an important role, but history is littered with stories of military forces that put too much faith in their walls alone. Savvy military planners have learned to use walls as merely one leg of defense: a dedicated fighting presence is still required to stave off a concerted attack.
Whether designed for military or civilian purposes, or a mix of both, wall construction is now standardized across the galaxy. As a rule, walls are built from the most advanced raw materials to withstand natural and unnatural calamity. Modern walls are also modular and scalable, and can be fit to serve whatever purpose their builders intend.