In the 1950s, Harry Harlow's experiment with infant macaques performed at the University of Wisconsin-Madison forever changed our ideas on animal emotions. One group of macaques infants were raise with a surrogate mother made of cloth. The other group was raise with surrogate mothers made of wire. Monkeys with the cloth mothers grew up to be fairly normal. Monkeys raised with the wire mothers grew up fearful and desperate. It was concluded at that point that there was something more than simply nutrition that was required for baby animals to be well-adjusted adults.
In primates, identifying the biological need to hug and to be hugged was reproduced during the most rigorous scientific experiments, over and over during the next 60 years. Eventually the study went beyond observable behavior and into the inner workings of the brain. Oxytocin (responsible for bonding, attachment) and dopamine (released in pleasurable activities) are the hormones behind these set actions. They are released when we hug and then are released again whenever we are around something or someone that we are attached to.
Animals that have higher social structures produce more oxytocin. This includes dogs. Studies show that dogs produce oxytocin when interacting with their humans: playing, petting, engaging in a group activity. However, in terms of oxytocin production, dogs respond either neutrally or negatively to any sort of physical restraint: hugging, reaching over a dog's head to pet, or collar restraint unless they are conditioned to accept this type of physical contact from an early age. These movements activate their hypothalamic-pituitary-adrenal (HPA) axis and huge levels of cortisol are pumped out. Cortisol, the major indicator of a response to stress, can remain in the dog's blood for hours in socialized dogs and days in dogs unfamiliar with human behavior.
Perhaps one of the most difficult things to hold back when we interact with dogs is our primate need to hug because it is a highly rewarded instinct. We crave it from birth, it is necessary for proper brain development, and we are rewarded for it with the production of all the feel-good neurotransmitters that happen when we bond. But it is our responsibility to control that impulse, especially when dealing with dogs that are feral, neglected, or from sensory deprived environments like puppy mills. We can get them to start bonding and having significant emotional attachment by engaging them in problem solving activities, being a source of predictable interactions, and providing them with routines to help them explore and understand their environment, and eventually petting and non-restraining snuggles.