Walls are created between any Watch Towers that line up with each other and are not blocked by other buildings. Walls prevent enemy forces from passing through them without having to first destroy them.
Gates are created whenever you enclose a space, you can tap on a wall to add or remove a gate but there must be a way for your population to move around your city.