The preselected 6 comes from the game's rough estimate of what would work well for the whole gym you are about to fight. If you are about to fight one fire pokemon and 3 water pokemon, it will select a team heavy with grass and electric, even if grass would do badly against the first pokemon you fight, or even if you have other pokemon with even much higher CP than what it chooses. I've seen it pass over a 1000CP Pinsir to chooses a 600CP Bulbasaur to fight a gym with lots of water types. I can only assume this is because the game is dumb, because that bulbasaur would only be as effective as a ~800CP pokemon with no type advantages.
The game also takes into account the moveset of the pokemon you have, choosing within a species the pokemon that have better moves to attack. In the above gym example, the game would choose a normal type Porygon with an electric move before it chooses a pokemon with no advantages at all, but it might choose a grass type pokemon with no grass moves because it would have strong defenses against the water types.
I also have one pokemon that is about 500CP higher than any other pokemon in my roster. The game is guaranteed to put that in every single line-up it every suggests to me, because that pokemon is at least 20% and about 50% higher CP than all my other pokemon. This means CP does become the main factor if the gap is large enough.
To a lesser extent, I believe IVs are considered when the game suggests a line-up for me to use. I've seen the game suggest 80%+ perfection pokemon over 60-75% perfection pokemon, even if it has lower CP and a worse moveset. This is the primary reason I never trust the game's suggestions unless it's an overwhelming force of CP.
To finally answer your question, there is logic behind the game's suggestions for your 6 pokemon to attack a gym. But that logic is difficult to understand at the moment, and in my opinion is flawed, so I suggest that you do not trust it and keep changing the team to what you want to bring and not what it suggests. You know your pokemon best, and you know what will do well against the gym you are about to face. You are better than the machine, which is why you are allowed to make those changes. Never give up that opportunity, even if it means a little extra work making sure your team is the best it can be against your target.