To me it seems to be a combination of your initiative, the enemy's initiative, status effects and defense skills, e.g. willpower, bodybuilding, etc.. Once a character is taken out it frequently ends up at the end of the queue and then gets worked back into the rotation based on its numbers. I know they use an algorithm to determine the order but I don't know what it is exactly.