Safety-critical Control with Control Barrier Functions: A Hierarchical Optimization Framework