Your beast companion has its own turn and can only act on commands during its turn.
The Ranger's Companion feature partially states (emphasis mine):
The beast obeys your commands as best as it can. It takes its turn on your initiative. On your turn, you can verbally command the beast where to move (no action required by you). You can use your action to verbally command it to take the Attack, Dash, Disengage, or Help action. If you don't issue a command, the beast takes the Dodge action. Once you have the Extra Attack feature, you can make one weapon attack yourself when you command the beast to take the Attack action.
From the second sentence it is clear the beast has its own turn separate from yours, and the rules consistently state that the ranger is commanding the beast on the ranger's own turn. The beast companion is a creature and like any other creature it takes its actions on its own turn in the turn order.
The beast's turn is stated to be on the ranger's initiative and by the rules of initiative (PHB, Chapter 9):
If a tie occurs, the DM decides the order among tied DM-controlled creatures, and the players decide the order among their tied characters.
I do not consider the beast companion a DM-controlled creature, but a character tied to a player and thus the players decide when the beast companion turn occurs relative to the player-controlled creatures with the same initiative. Generally you would choose the beast's turn to directly be after yours, otherwise you can not command it and it will take the Dodge action. But if you are incapacitated it can be favorable to let the beast go first, or if the initiative tie includes other player characters let them go in between you and the beast.
In conclusion, the ranger can take the Attack action to make one weapon attack and command the beast, then take a bonus action. The beast will take its action on its own turn according to the command it was given, which is generally after your bonus action.
To clarify: So that would mean I can use shield master's ability in a bonus action before my creature attacks or?
– Haakon Jun 02 '22 at 08:11Interesting idea by the way: Now imagine that you have a giant crab as a companion that "Grapples" the opponent. Getting up will then be quite difficult for him. xD
– Haakon Jun 02 '22 at 09:01