Every religious doctrine has taken it's own path and philosophy to explain where we come from. Per Hinduism, it is NOT a religion, it is just a way of living life, it defines the ways that will allow you to live a happy life.
Each Gotra searches it's roots to an ancient sage as originator of that Gotra. The word Gotra means Cattle Shed in Sanskrit, refers to comtemporary lineage segment for a joint family. In those times, cattle was one of the important symbol of wealth, the word Gotra may also be deviced to define the lineage and way to claim inheritence. Panini defines gotra for grammatical purposes as apatyam pautraprabhrti gotram, which means "the word gotra denotes the progeny (of a sage) beginning with the son's son.
The Gotra is a lineage system of a person with his most ancient ancestor in an unbroken male lineage. For instance if a person says that he belongs to the Kaundinya Gotra then it means that he traces back his male ancestry to the ancient Sage Kaundinya. So Gotra refers to the Root Person in a person’s male lineage.
Gotra originally referred to the seven lineage segments of the Brahmans, who trace their derivation from seven ancient seers: Atri, Bharadvaja, Bhrigu, Gotama, Kashyapa, Vasishtha, and Vishvamitra. An eighth gotra was added early on, the Agastya, named after the seer intimately linked up with the spread of Vedic Hinduism in southern India. In later times the number of gotras proliferated when a need was felt to justify Brahman descent by claiming for one’s line a Vedic seer. Eventually other Gotras were added to accomodate other lineages. It may also be possible that, other Gotras were existent but were recognized or number of descendents grew to considerable number.
A gotra must be distinguished from a kula. A kula is a set of people following similar cultural rituals, often worshiping the same divinity (the Kula-Devata, god of the clan). Kula does not relate to lineage or caste. In fact, it is possible to change one's kula, based on one's faith or Ishta-devata or Kula-Devata (Family worshipped god / goddess).