I'm not very experienced with strategy games, sins is my first one.
We all have to start somewhere.
what I should do at a certain point into the game
In all honesty, that's a very tough question to answer. Even experienced players get stumped from time to time. Oh, sure, with 20/20 hindsight you can always pick out what you should have done differently, but in the heat the moment everyone has to make though decisions, and even the best of us can make wrong ones.
One good idea is to stay one step (or two or three) ahead of the game. Think about what you're going to do next so you have your long-term plan laid out, and perhaps have a head-start on it.
Then, I'm stuck. To a point, I want to buy emergency facilities and make defenses to my border planets. But should I attack, or entrench more (I play Entrenchment)?
It shouldn't surprise you that every situation is unique. Sometimes you attack, sometimes you defend, sometimes you seek out more planets that are further away, and sometimes you build up a powerhouse economy. It all depends on the situation the map provides and what your opponents are doing.
In any case, on to the problem at hand: you're having trouble because of counter-attacks. Yes, this is the price of attacking: you are vulnerable to a counter-attack. I think I'll start by stating the obvious: if you're fighting two fleets of equal strength to your own, don't expect to win. If you have two flanks that are sufficiently distanced, and the enemies on both sides are attacking you (and don't have their own flanks to worry about) then you basically have no other option but to defend. It's a simple matter that you're in a 2v1 scenario, and you're out-gunned. Find cover.
Now, it's entirely feasible to juggle your assets to fight two wars at once against two powerful fleets, but typically this is either a late-game phenomenon where your empire is that much stronger, or else you're a much better player than those opponents. The AI isn't very good at attacking starbases, so if you find a nice choke point and set up a starbase there with repair bays, it will probably repel just about anything the AI sends at it while your fleet is free to attack the other front.
It's pretty difficult to keep track of where everyone is all the time, though. If I'm attacking, I try to maximize the effectiveness of my attack, and don't constantly scan other planets to figure out where other's fleets are.
Keeping track of everything is tough, but it needs to be done. That will come with time.