TL;DR While the cow has always been treated with veneration in Hindu mythology and literature, the roots of the sacred status that it now enjoys are primarily because it was a convenient symbol of mobilization, first to counter the rising tide of Buddhism and then in the nationalist pre-independence movement. Rational arguments based on the economic value of the cow to a rising India were primarily an afterthought justifying more primitive religious and cultural arguments-- though the cow was a great source of wealth and value for millennia.

The long version:

A few snippets from the past:

Mythology and Literature

In the Puranas, the earth goddess Prithvi (a cow) was milked by Prithu to end a famine and provide food for humans. It also mentions Kamadhenu, the mother of cows, the source of prosperity.

The Rigveda calls cows aghnya- "that which may not be slaughtered". Several stanzas indicate that cows were treated with reverence and should be protected. This is possibly because cows were heavily relied upon- for their products (dairy, dung)- and treated as the caretaker of the home- gau mata. A vast array of words were dedicated to refer to them in different stages.

Many stories revolve around Krishna, a.k.a. Gopala "the cow protector". Cow milk is stated as Sattvic (i.e. cleansing).

History

Vedic period: 1500 - 500 BCE (sourced from Brown) Cattle were a measure of wealth, and a key part of war booty. The usefulness of the cow/ox in terms of milk, flesh and agricultural assistance are stressed in the Rig Veda. Cattle had ritualistic significance too; they were the main sacrificial animals, and their products were used as offerings. Scholars debate whether the cow is beginning to attain inviolability, for all but the Brahmans. The end of the Vedic period coincides with the rising popularity of the Ahimsa (do no harm) philosophy, as well as an increased literalism in the sanctity of cows, by some records.

500 BCE - 500 AD Manu's laws forbid the eating of consecrated flesh (that which is not sacrificed); though he stresses the value of the cow and lists her many virtues, this still seems like a part of the broader Ahimsa philosophy. He who eats the meat of any animal in this world will be eaten by that same animal in yonder world.

According to legend, a Chola king who was a follower on the Manusmriti executed his son to provide justice to a cow.

The Mahabharata (~400 BCE) states that he who kills a cow lives as many years in hell as there are hairs on the cow's body, basically urging you to shave the cow before killing it. Both Ahimsa and cow worship are treated as established doctrine and extolled as desirable.

The Arthashastra (~200 BCE) recognizes meat-selling as legal, but states that cattle were not to be slaughtered, possibly unless they were economically worthless.

By the 4th century AD (Gupta dynasty), the sanctity of the cow is established in Brahmanical circles.

Much of what happens between 500-1500 AD is similar-- the cow is venerated in Hindu kingdoms, but is often sacrificed or killed to honour guests. Beef-eating is largely treated as a sin.

1500 - 1857 (Mughal Empire) During the Muslim invasion, Hindus are recorded as often being shocked by cow-slaughter.

In the 1600s, Maratha (Hindu) chieftain Shivaji treated cow protection as a key part of his rule.

1857 Rumors of cartridges being greased with cow fat lead to a Sepoy mutiny.

1882 Swami Dayananda Saraswati establishes Arya Samaj and publishes Gokarunanidhi where cow slaughter is opposed as anti-Hindu act. They are helped by the rise in easy transport (trains, buses) and printing presses to spread the message. The first Gaurakshini sabha (cow protection society) is set up in Punjab.

1888 The Allahabad High Court rules that cows are not sacred as per section 295 of the IPC and hence their slaughter was not illegal.

1893 An estimated 31-45 riots over six months broke out between Hindus and Muslims, mainly in present-day UP, but also in other parts of the country. 107 people were killed.

1930s-1948 Gandhi - "The central fact of Hinduism is cow protection"

1950 The Directive Principles of the Constitution of India suggest that cow slaughter should be abolished.

1966 "Hindu" organizations led by a fasting Shankaracharya demand a ban on cow slaughter as per the (unenforced Directive Principle). A mob of allegedly over 10000 people tried to storm the parliament, eventually rampaging through New Delhi, leading to a 48-hour curfew.

All of this and more is described in great detail in:

Mukul, Akshaya. Gita Press and the Making of Hindu India. HarperCollins Publishers India, 2015.

Further Analysis

Brown, W. Norman. "The sanctity of the cow in Hinduism." The Madras University Journal 28.29 (1957).-- cows were offered in sacrifices, and were not inviolable in Vedic times. This is an excellend, short reference that answers the bulk of this question (valid until 1964).

Jha, Dwijendra Narayan. The myth of the holy cow. Verso, 2002. deals with the early history, but glosses over medieval India.

Ambedkar, Bhimrao R. "Untouchability, the Dead Cow and the Brahmin." Collected Works of BR Ambedkar 7 (1979): 185-207. -- For the Brahmin everyday was a beef-steak day. The Brahmins were therefore the greatest beef-eaters

Describes how the roots of Brahmins vegetarianism lies in the conflict between Buddhism and Brahmanism. Eventually, the "holy cow" became a cultural symbol for mobilizing Hindu nationalists.

You may also wish to read this post describing how the cow gained political significance in modern India. This paper describes a debate on whether the cow is sacred for "irrational" or "calorific" reasons.