I have a large illustration with several groups and subgroups of objects. I want to flatten everything and separate the whole illustration by colors. I tried merging the artwork into one layer and clicking trim but nothing got trimmed. I also tried flattening the artwork and clicking trim but nothing happened. Am I supposed to use another tool in the Pathfinder? What am I doing wrong?
If I understand correctly, you need to "ungroup all" repeatedly, assuming you've already used pathfinder to subtract shapes as necessary for overlaps and knockouts to look correct once you've layered by colour.
Once you've done these two steps, then select one element of colour XX, go to Select>Same>Fill Colour, and using the layers palette, pull your elements into the new target layer.
Bear in mind you could also do this without ungrouping by using the Direct Select (White Arrow) tool for the initial selection (being careful to select the whole item versus the segment or anchor point of course) and then completing the same Select>Same>Fill Colour workflow.
Hope that helps.