There are many versions of how taps originated.  Here are a few of them.
 
The bugle call Taps  had its origins on a battlefield of the Civil War. After the Union suffered a large number of casualties in a battle near  Richmond, Virginia, Brigade Commander Colonel Daniel Butterfield  reflected with sadness upon the men he had lost.  Unable to compose music, he
hummed a melody which his aide wrote down in musical notation.  The company bugler played it that night to honor their dead comrades.  It was officially recognized by the United States Army in 1874.  Accompanied by the drumbeat, Muffled Ruffles,  it is the highest honor given to those who have died in service to our country.

On June 27, 1862 during the early days of the Civil War, soldiers of the 83d Pennsylvania Volunteers began to fall to left and right during the battle at Gaines' Mill, Virginia.  At a critical moment of the battle when the flag was in peril, Brigadier General Daniel Adams Butterfield rushed forward to secure the flag and encourage the embattled troops to renewed valor.   For his own valor, General Butterfield was awarded the Medal of Honor. During the Peninsula Campaign of that same year, General Butterfield with  assistance from his bugler Oliver Norton, wrote a song that became known as taps.  Originally used to signal the close of day and time for the troops to put out the lights for the night, "Taps" became a soldier's salute to his fallen comrades.  Today it is not only played during ceremonies to lower the flag at days end, but is used in military funerals as the final salute of the living to those who have served and passed on "into the night".
 

It all began in 1862, during the Civil War, when a Union Army Captain, Robert Ellicombe, was with his men near Harrison’s Landing in Virginia. The Confederate Army was on the other side of this narrow strip of land. During the night, Captain Ellicombe heard the moan of a soldier, who lay mortally wounded on the field. Not knowing if it was a Union or Confederate soldier, the Captain decided to risk his own life and bring back the stricken man for medical attention. Crawling on his stomach through gunfire, the Captain reached the soldier and began pulling him back toward his encampment. When the Captain finally reached his own lines, he discovered it was actually a Confederate soldier, but the soldier was dead.

The Captain lit a lantern, suddenly caught his breath and went numb with shock. In the dim light
of the lantern, he saw the face of the soldier…his own son! The young man had been studying music in the South, when the war broke out, and without telling his father, he had enlisted in the Confederate Army.

The following morning, the heartbroken father asked permission of his superiors to give his son a full
military burial, despite the young man’s enemy status. The Captain’s request was partially granted. He asked if he could have a group of army band members play a dirge for his son at the funeral. His
request was refused, since the soldier was a Confederate. Out of respect for the Captain, they said
they could loan him one musician. He chose a bugler. The Captain asked him to play a series of notes found in the pocket of the dead youth’s uniform. The wish was granted. The music was the haunting bugle melody we know as "Taps".
 
 
The most recognized rituals of a military funeral include draping the casket with the flag, cannon salutes, firing three rifle shots across the gravesite, a 21-gun salute, and the playing of Taps.

Of course, the latter is a truly American bugle call, with roots in the Civil War when it earned its nickname because it was often tapped out by a drummer when no bugler was available. The tune was composed by Union general Daniel Butterfield in 1862 who penned the tune to replace "Tattoo," another "lights out" call which he felt was too rigid. He was stationed in southern territory at Harrison's Landing, Virginia, and perhaps that's how the tune came to be played in both North and
South camps.

At first the call was reserved to simply signal sundown in the camps and its first use at a funeral was more out of military strategy than anything else. In 1862, according to a manual later published by a Union officer, a soldier was buried at the same site his battery had occupied a position not far from enemy lines. The custom was to fire three rifle shots over the grave, but due to the forward position of the troops, the captain in charge decided against the rifle shots and felt the playing of "Taps,"
since it was solemn and also would not alarm the enemy into thinking a attack was coming, would be a worthy substitute. It quickly became a custom at all services for Union soldiers and was then officially decreed by the heads of the Army of the Potomac.

"Taps" was officially adopted by the United States Army in 1874.