I've been given that advice many times and it took some deep introspection and many years to figure out what it meant and how to be myself. This might be self evident for many people, but for me discovering myself meant knowing what made me happy and unhappy. I've done that by being mindful of how I felt before/while/after doing something. This let me establish a core persona who is more influenced by past history and less by current situation. At this point just be myself is a good reminder, but starting with that advice was not helpful at all.
The time when people are least "themselves" is when we're entrenched in established groups, trying to stay "Liked", following the group-think, not risking status, saying what we think people want to hear.
The risk of being excluded from an existing group as a "defector/traitor" feels much worse than of "not being accepted as an outsider" when approaching a new group since we expect some latitude. Being the new kid can give you some charm and opportunity.
However, it's within existing, rigid structures that speaking your mind (against consensus) can have the most extraordinary effects. It's a huge risk and a fine balance, but someone who has earned respect, and has "standing", can be themselves while also being part of the team then they have become something much more - a leader.
Not everyone knows who or what they are, and even someone who knows is not necessarily able to be themselves without outside help.
I struggle with being myself quite often; there are things I want to do, but I can't simply decide to just do those things... My brain does not work that way. For example, it can take a lot of effort to even say what I truly want to say, because consciously deciding to speak does not necessarily result in me actually doing so, no matter how hard I think the thought.
I wanted to belong and did things to be cool and accepted, without really understanding the mechanisms behind it.