In the core book for 5e D&D, it says you have to command your pet to take an action every round, so on the surface it would seem that if you are knocked out or are dying your pet will not be able to protect you/continue fighting. This doesn't make sense to me since the animals can react instinctively to danger and other stimuli, so one would think it wouldn't be unreasonable to let the ranger pet continue attacking after the ranger is incapacitated, either by protecting the body of its owner from continued threat, or possibly lashing out at seeing his master injured.
So the question is: Is there anything in any of the books that accounts for the animal companion's own ability to make decisions, including continuing to attack without being explicitly commanded?
I expect the answer to this question might be "if the ranger is unconscious it would be imbalanced if the pet got to attack, since that allows the player to essentially continue fighting even after they have been knocked out/killed". But I think it's reasonable to allow the animal to protect its master, or maybe it just starts attacking random people, even allies, since its not being directed to a specific target by the ranger.
I am preferably looking at a 5e RAW answer, but am open to possible house rule options based on previous editions rules if it seem non-game-breaking in the 5e context.