The phrase originated as 'son of a military man' (that is, a gun). The most commonly repeated version in this strand is that the British Navy used to allow women to live on naval ships. Any child born on board who had uncertain paternity would be listed in the ship's log as 'son of a gun'. Read more about this phrase and find out if the Navy allowed women on board here.
Today the phrase means the boy is naughty.