Perfectly keeping up macro while simultaneously microing your troops is the holy grail of starcraft mechanical skill. Even top players can let macro slip while they micro, although at that level, the slips are in fractions of seconds. There is no secret technique that will make you perfect at this skill, but you need to get in the habit of doing 'macro cycles'.
These are pre-determined cycles of tasks that you repeat, over and over again, all game. The specifics of your cycle will depend on your race, your build, and the time passed in-game. The cycle will start simply, but become increasingly complex as you approach mid and late game.
Micromanagement of troops takes place in-between your cycles. Do your cycle, then attack-move your troops towards the point of attack, then do your cycle again, then micro the attack, then quickly go back to your cycle when you have a moment.
An example of a simple cycle for Terran early game:
1-click minimap to focus on your CC, throw down a mule, queue one SCV
2-click your production hotkeys, get one marine queued for each barracks
3- check supply. if needed, click f1 or choose an scv, build a supply depot
4- doubletap your main troops hotkey or click minimap to go back to controlling your army
You repeat that cycle every 20 seconds or so, as needed. Your cycle gains complexity when you get more production facilities or expansions. But in any case, the faster you can do a cycle, the more time you have to focus on your troops, scouting, and the dynamic aspects of each game.
When you get very practiced at it, you will be even be able to take your focus away from a battle for a few seconds, to do a quick macro cycle, thus maintaining an economic lead during heavy engagement.