You must make a successful ranged touch attack to hit your target with the ray, which deals 2d6 points of acid damage, 2d6 points of cold damage, 2d6 points of electricity damage, and 2d6 points of fire damage.
The type of energy that does the most points of damage to the target then persists, dealing another 4d6 points of that type of damage per round for 1d4 rounds.
When I closely read the final part, its the elemental type that does the most damage. So if I happened to roll really well for a damage type that its not immune or resistant to, but also not its weakness, its weakness isnt guarenteed to be that type.
What happens when two or more energy types deal the same amount of damage, say when you maximized the spell? They all did the most damage, so should they all not persist?