When you are ready to improve your defenses to protect your resources, there are several things to consider. Your defensive towers range must cover all parts of your city. Place your structures close together to make them easier to defend, as well as to be secured with walls. Upgrade your walls and defensive towers as you progress.
Start by improving your resources. You have three primary resource; food, stone and wood. Collect them from farms, stone quarries and woodcutter mills respectively. Build the maximum number allowed for each of those structures, as well as upgrading them for maximum output. You should also build more houses to increase your population as part of improving your resources. You do not have to worry about building your defenses until your resources get large enough to get the interest of other players.
When setting a specific time to collect taxes, the more citizens you have and the longer you set the time for, the more gold you will earn. However, you must return to the game after that specified time finishes. After the time period ends, you will slowly lose gold. The longer you take to get back and collect the taxes, the more gold you will lose.
There are attack and defense tools that can be used to increase your odds. For example, the Scaling Ladder gives a defensive increase when used against the castle walls. The Battering Ram helps units that are going against castle gates.