How does judicial discretion in granting bail impact the balance between individual rights and state security?

Recent verdicts, like the Umar Khalid case, show that courts sometimes deny bail even after long incarceration. I want to understand how judges decide on bail and what principles guide their discretion, especially in sensitive or political cases.