What is the logic behind auto-recommended 6 Pokémon when you enter a gym battle?
Solution 1:
It seems that the way it preselects these 6 is by finding the best type matches for the opponent. From what I've seen it tends to base this on typing and movesets, then sorts by CP and HP.
That said, I haven't seen it suggest what I would call the "optimal" 6 Pokemon for the entire fight. It looks like it selects all 6 to combat a single opposing Pokemon, typically the first one you will fight. It seems like it merely chooses an advantageous type matchup Pokemon with the highest CP then goes down the list.
I was able to find some evidence to support what I've said here and here.
Solution 2:
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.