How come do we attribute general theory of relativity to Einstein when David Hilbert published first?
This seems a bit of a naive question to me. Einstein had been working on this problem for several years (starting as early as 1907), and had developed much of the physics by 1912. He greatly struggled to find the correct mathematical formulation of his theory, finally discovering the necessary tools from differential geometry during a collaboration with the mathematician Grossmann in 1912-1913. However, Einstein's ideas on e.g. the equivalence principle were already known in the community by the time he and Hilbert finally found the correct formulation in terms of the Einstein Field Equations or, alterntively, the Einstein-Hilbert action. It is therefore only natural that the physical theory is regarded as Einstein's.
First, to echo and elaborate on previous answers, Hilbert only appeared on the scene during the last few minutes of the last act, so to speak.
Einstein made his first step towards the General Theory of Relativity (GR) in a 1907 paper "Relativitätsprinzip und die aus demselben gezogenen Folgerungen" (On the Relativity Principle and the Conclusions Drawn from It), in which he first introduced the Equivalence Principle; he later called that "The happiest thought of my life". In 1911 he returned to it in the paper "Über den Einfluss der Schwerkraft auf die Ausbreitung des Lichtes" (On the Influence of Gravitation on the Propagation of Light) (This paper is available in English translation in the collection The Principle of Relativity.) In that paper he calculated the bending of a light beam passing close to the sun, getting half the correct value.
Both these papers were a response to the clear incompatibility of Newton's theory of gravity with Special Relativity.
Einstein continued to work on the theory after 1911, eventually realizing the need for the mathematics of differential geometry. There followed a long series of papers where Einstein (with help from Grossmann) overcame various misconceptions. Pais's biography Subtle Is The Lord... gives a detailed technical discussion of the path Einstein traveled.
In June and July 1915, Einstein spent some time in Göttingen, giving lectures on his work and having prolonged discussions with Felix Klein and David Hilbert. In Oct-Nov 1915, Einstein surmounted the final obstacles and obtained the field equations of GR.
Independently Hilbert obtained the field equations, via a somewhat different approach. (As Pais points out, Hilbert had to overcome his own, somewhat different, misconceptions.) During the crucial month Nov 1915, Einstein and Hilbert exchanged several letters. The publication by Hilbert and by Einstein of the field equations was nearly simultaneous (5 days apart).
So Einstein had made a whole series of fundamental conceptual advances, before Hilbert ever became engaged with the topic. Hilbert had the benefit of Einstein's personal discussions. Incidentally, Hilbert also needed a crucial assist from Emmy Noether with the math (the famous Noether's theorem on symmetries came out of this).
Felix Klein wrote, regarding the field equations:
there can be no question of priority, since both authors pursued entirely different trains of thought to such an extent that the compatibility of the results did not at once seem assured
and Pais's final verdict:
I do believe that Einstein was the sole creator of the physical theory of general relativity and that both he and Hilbert should be credited for the discovery of the fundamental equation [i.e., the field equations]
Finally, I should mention the "priority dispute"; the Wikipedia entries "Did Hilbert claim priority for parts of General Relativity?" and Did Einstein develop the field equations independently? contain a detailed discussion.
Michael Weiss's answer is excellent. However, some clarificaiton is needed. Historians as late as 1997 went back to reevaluate the priority dispute and, the same way Newton created differential and integral calculus before Leibniz did (albeit independently), Einstein arrived at the correct field equations before Hilbert did (weeks before, not five days):
Hilbert's published equations were not generally covariant, Einstein's were, and it took him a few weeks to sort it out. Excellent point on the brilliant Emmy Noether (the most under-appreciated scientific genius of the 20th century). I also think the question is a bit silly given the fact that without Einstein's lectures at the University of Gottingen in 1914 and his numerous letters to Hilbert, Hilbert doesn't even come close to finding the final field equations. In a way, Hilbert's behavior during the entire episode was a bit unethical.
Also, Hilbert's general theory of gravitation (he created his own) is riddled with errors - pointing to the fact that one can be a mathematical genius without necessarily being a scientific one. His conceptual understanding of physics was somewhat lacking.