Creatures with low mental ability scores can still distinguish friend from foe
Since the text of Polymorph doesn't clearly answer this, let us look at some other examples. As an extreme example, we have the Feeblemind spell (emphasis added):
On a failed save, the creature's Intelligence and Charisma scores become 1. The creature can't cast spells, activate magic items, understand language, or communicate in any intelligible way. The creature can, however, identify its friends, follow them, and even protect them.
Even with the lowest Intelligence score possible, a creature affected by Feeblemind can still identify its friends. For a more mundane example, we can look at the stats for an actual beast, such as a wolf. A wolf has an Intelligence score of 3, and yet it is clearly able to identify its friends, since wolves live and hunt in packs. In fact, the wolf's stat block even includes a mechanical ability that requires it to be able to identify its allies:
Pack Tactics. The wolf has advantage on attack rolls against a creature if at least one of the wolf's allies is within 5 feet of the creature and the ally isn't incapacitated.
So, nothing about the transformation into a beast or the associated reduction in mental ability scores makes it inherently impossible for a polymorphed creature to distinguish friend from foe. Given that the spell also says the creature retains its alignment and personality, this means it would retain its desire not to hurt its friends and to defeat its enemies.