Thank you for posting this in Microsoft Q&A.
Error code: AADSTS51004 is states the user account doesn’t exist in the directory. An application likely chose the wrong tenant to sign into, and the currently logged in user was prevented from doing so since they did not exist in your tenant. If this user should be able to log in, add them as a guest. For further information, please visit add B2B users. Make sure that the user account email is added to the directory before you can sign into the application.
Reason behind the error you saw is, that when you created these users via google, their email became their immutable, and it worked fine, however if you create users directly in Office 365 /Ad sync, then you would first need to ensure immutable = user’s email address. I hope this information helps! If you have any further questions, please feel free to ask.
For more information, please refer https://learn.microsoft.com/en-us/answers/questions/465354/erro-aadsts51004-ao-configurar-integra-o-do-gsuite
https://learn.microsoft.com/en-us/education/windows/configure-aad-google-trust
Let me know if you have any further questions.
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.