This use of 'had' is a subjunctive form, used for a wish or a (maybe impossible) desire. I don't have a dog, in reality, but I am imagining a hypothetical or unreal situation in which I had a dog.
Don't confuse this with the past tense 'had' - the form is the same, but the use is different. This is not an English past tense.
Also, don't confuse this sentence with statements such as 'I want to have a dog' or 'I'd like to have a dog', which are about 'real' situations, and use the infinitive.