In Clash of the Clans, to minimize loss of resources when your base is attacked is it better to store loot in clan castle and not collect it? Also is it better to leave resources in elixir collectors/mines for as long as possible before collecting it (at what point is it necessary)?
It's better to collect. You can defend your stores much easier than you can all your collectors.
And of course, spend it as soon as possible. Queue up troops etc.
Also, an advantage of having a side town hall ( in a corner with no defenses ) is quite often it will get sniped ( someone will just drop a few archers and destroy just the town hall ) and you will get a shield for 12 hours which will protect your resources.
Without question, the best place to keep your resources is in the Clan Castle and Storage buildings.
All information can be referenced from the Clash of Clans Wiki Raid article. Here are the percentages of loot that can be stolen during a raid. Keep in mind that there are also loot caps, which differ depending on what building is storing the resources and what Town Hall level you are at.
Collectors/mines: 50% - no cap beyond the capacity of the mine/collector Gold/Elixir Storages — Percentage depends on Town Hall Level Level Percentage Cap 1-5 20% 200,000 6 18% 200,000 7 16% 250,000 8 14% 300,000 9 12% 350,000 10 10% 400,000 Clan Castle Treasury — Flat 3% at all Town Hall levels on destruction
For completeness, I'll include information for Dark Elixir as well.
DE Drills: 75% - no cap beyond the capacity of the drill Dark Elixir Storage — Percentage depends on Town Hall Level Level Percentage Cap 7 6% 1,200 8 6% 2,000 9 5% 2,500 10 4% 3,000
There's one or two other things that can affect how much resources are available during a raid, but they are broad and apply to all resource buildings equally. As you can see for gold/elixir, the Clan Castle offers the lowest percentage of available loot, followed by the storage buildings. The Dark Elixir storage is just slightly better than the clan castle when it comes to protecting Dark Elixir, but it is probably still optimal to keep the resources in the Clan Castle to protect gold/elixir and the benefit of not keeping all your DE in one "basket", so to speak. But keeping resources uncollected in the mines/collectors/drills is certainly the the worst place to leave them.