Some basic pointers:
Make more money and have more factories and labs. Infrastructure as GW said. I know, not always so easy. Basically you need to have better ships which means more research than that AI. This means that you will want to not go to war too soon. The best ways are getting potrential enemies to fight eachother (again, as GW said). You can also resaerch some diplomacy techs or have an early military which will help keep the bad guys away. Sometimes no matter what you do you will eventually get attacked.
I never have a good military early in the game, never. I build up my planets and economy and have other fight wars. I wait until the inevitable attack comes. But by then i can quickly build ships that i need. Usually I'll lose a bunch at first. So don't worry about it. Remember, you only lose something if you actually lose a planet. Otherwise its just a ship or two.
Research upto a good weapon and then put more into your military sklider to get some ships out faster. Better defences, hulls, speed, miniturization etc. are necessary at some point but you have to decide if researching more is better than getting some defence out there first and then getting better stuff later.
I hope this helps a bit. But it might help too if you gave specific problems or questions.