Just out of curiousity, I went to the UCI site to see what the rules have to say but I found nothing. Other searches indicated that it is not just grade or distance but elevation, point in the stage, are there any flatter areas to provide a bit of relief, etc. Furthermore, since it is determined by the race director, a Cat 2 climb in one race might be a Cat 1 in another.
One quote that I thought was particularly amusing was:
I read an interview by a TDF director that it was to do with which gear the Tour Director's car was in to go up it, and if it overheated or stalled it was given an HC category!