Part of a solution for that are term-limited civil service jobs - that keeps a rotation of talent and low performers can't stay forever.
That said, you raise an important problem - how do you manage poor performers without allowing a bad-faith political official to fire people unfairly?