The starting point is that you decide for yourself. With every decision, a counsellor will reassess whether you can make a conscious choice. If yes, then the choice is yours. If not, your legal representative can (co-)decide.
Care providers can usually assess for yourself whether you are able to make your own decisions during treatment and care. They often consult with other care providers in the treatment team. In case of doubt, an independent assessment will follow.
A non-treating physician (such as a geriatric specialist or psychiatrist) will then examine whether or not you can make an informed choice about your examination or treatment. They ask you certain questions to properly assess whether you can decide on this and are therefore legally competent. You can also be temporarily incapacitated, for example if you are unconscious.