The walls with towers and archers/skirmishers can hold off most attacks until the castle age. Support with a group of knights to rush out and attack the siege weapons and you should last awhile longer without going on the offensive.
With your ally switching sides on you send a villager to build a gate, or just wall it over, in front of all their gates and lock it and destroy their gate. This will keep them from letting the enemy thru and also allow for you to get thru when needed by unlocking the gate.
The Teutons are a good civ. for defense due to the 2X capacity on the towers.
The AI giving up before complete annihliation usually happens if you wipe out all the military buildings so if you have a large group of archery then wipe out all but a barracks building and leave it as the last building to destroy. You also have to leave a few villagers to allow for razing all the buildings.