In CvPlayerAI, AI_doTurnPre() (called at the start of each turn) calls AI_doCivics. AI_doCivics makes sure the AI is allowed to change civics, and hasn't swapped too recently, then assuming that's true tries to shift into the civics that evaluate highest on AI_bestCivic(). AI_bestCivic() gets a score for each civic based on AI_civicValue(), then increases the value of current civics by x1.2 if it would take non-zero turns to switch civics, or x16/15 if switches are free (to make AI civic choices a little "sticky").
But the bulk of the work is done by AI_civicValue, which is about a 300-line function adding up values for various things that civics might give. Avoiding emancipation anger is worth points. Additional GPP rate is worth points. Reduced military upkeep is worth points. Reduced corporate maintenance is worth points. And so forth, and so on. Big long list. "Favorite civic" adds a pretty hefty boost on to the score at the end of the evaluation, which is why AIs tend to be in their favorite civics - although if the game situation was really favorable for something else, from their perspective, they could switch.