Raid and War Selections...how are they chosen?

For wars see the following:

For raids, the opponents are chosen purely based on the number of trophies you have, you will get matchups with players within +/- 300 trophy range.