How do I decide how many hit dice a custom creature has?

I am planning to create some custom monsters for my games. I know how to determine a monster’s average hit points, however I don’t know how you determine the decomposition in hit dice.

For example if I create something similar to a Goblin I am able to tell it should have 7 hit points but I don’t know how 2d6 is determined.