In Shiva maha puranam , Varaha Puranam ,Kurma Puranam and linga Puranam they explained about Why Nandi is always with Lord Shiva.
Shilada’s Penance for a son
One there lived a Sage name Shilada who is well learned in Vedas and shashtras. He wanted a son with the grace of Lord Shiva and began a Penance for a son. His penance continued for thousand years. Lord Shiva became pleased with Shilada’s penance and asked him the reason behind the penance. Sage Shilada asked a son who is virtuous and knowledgeable in Vedas and Shasthras and specially mentioned that he want a child who is not born from physical entity. Lord Shiva agreed to Shilada’s request.
Soon Shilada went to his hermit and performed a Yagna from which a child appeared from Hawan Kund. The Child was named as Nandi. When Nandi was around seven years of age, he became proficient with Vedas and Shasthras. One day two brahmins came to Nandi’s home and warned about the “Mrithu Gandam(death)” of the Nandi which comes after a year. Listening to the Brahmins, Sage Shilada became sad. Seeing his father’s sadness, Nandi started a penance at the shore of Ocean to take help of lord Shiva
Nandi’s repeated Penances
Nandi then chanted a secret mantra for one crore times after which Lord Shiva appeared before him. Nandi then asked Lord Shiva to grant him a life where he can chant the secret mantra for one crore times more. Lord Shiva agreed to it. Nandi then started his penance again with chanting the secret mantra for 1 crore times. Lord Shiva appeared and Nandi asked for same boon. Shiva agreed and went away. Again Nandi repeated the penance and asked the same wish.
Lord Shiva made Nandi his constant companion
This time Lord Shiva said “now stop this penance i will make you immortal and make my constant companion and made the leader of Ganas “Ganadipaati”. The place where Nandi chanted the mantra became famous for Japyeshvar thirtha.
Lord Shiva gave the garland to Nandi to wear and blessed him with similar features of Shiva himself. Lord Shiva then took some water from his locks of hair and sprinkled on Nandi . That water which fell on the ground when sprinkled emerged as five ponds which met and became a river which is later known as Panchand. Both Lord Shiva and Goddess Parvati accepted Nandi as his own son and took him along with them after Shilanda’s death. Later Shiva himself arranged the marraige of Nandi to Suyasha who is daughter of Marut.