Arranging the walls around your vital buildings is the key to defense. The arrangement of defenses and other buildings inside the walls is also important (don’t leave any gaps where the attacking forces can spawn inside your walls!), but walls give your defenses time to cut down invading units. Set up your defensive buildings so their fields of fire overlap.
Get creative with your wall placement. Set up multiple layers of walls that enemies must breach to reach their targets. Walls can also be more than simple barriers to enemy units. Leave narrow paths that funnel enemies through twists and turns lined with invisible defenses, such as traps and mines.