I believe this question is fundamentally the wrong way up; as a result of how it's constructed, the UK is a thin democratic layer on top of various parts of what I would call the "permanent state": the Crown, various public sector bodies, local government, the police, etc.
Within that the government (effectively, the PM plus cabinet) has substantial power under the Royal Prerogative, constrained by statute law and judicial review. Those constraints also apply to the civil service: generally statute law specifies how administrative regulation is supposed to work, with authority over the details granted to the relevant Secretary of State.
The "Yes Minister" argument is that civil servants have substantial control through the "boxes" and the information they can present to ministers. This is true, but it has eroded a lot in recent years - not only through the internet, but also through the "SPADs", who are political advisors that report to the Party not the civil service.
Ultimately civil servants who unambiguously refuse to obey clear written instructions from the Minister may be found in breach of the Code, and sacked as ordinary insubordinate employees - but at higher level they may be hard to replace, and as you remember Humphrey never disobeyed a direct order. Parliament is also free to pass laws obliging certain duties to be performed in certain ways, in which failing to do so may be a criminal offence, although I think this is very restricted (Official Secrets Act comes to mind as an example).
Possibly the most egregious example of different parts of the government working against each other illegally was the Matrix-Churchill affair. There are probably other examples from Northern Ireland where the security services were allowed to run a police state for decades; I remember hearing that government negotiations with Sinn Fein for the ceasefire had to be kept secret from Special Branch. There are also persistent rumors that MI5 were plotting a coup against Harold Wilson, and Special Branch following Peter Hain around.