So basically you have two questions here:
Why use the military spending share of the GDP as a measure?
The total military spending as percentage of the GDP has been long been a measure to evaluate the so-called status of burden-sharing within NATO, see e.g. this report of the GAO from October 1990:
Measuring each ally’s share of the burden has caused considerable debate and disagreement within the alliance. However, two economic measures - percentage of and per capita gross domestic product spent
on defense - are among those most commonly used.
The %/GDP is a simple measure that is easily calculated by looking up two numbers in the common yearly statistical reports. GDP is apparently chosen as this number is the most common one that's ubiquitously available.
Also, for checking historic trends it is better to stick to the same measure (or at least, to keep it besides better measures, as older data for those probably is unavailable).
Why to choose 2% as the specific threshold?
Such decisions are usually made gradually, by proposals from one side which are discussed in think-tanks and at unofficial meetings and official NATO summits, so probably it is difficult to trace down all the history of the decision.
The comparison of military spending as an assessment of burden sharing thus has long been a common practice, see the aforementioned report. However, when this became an institutionalized measure that is brought up in the official NATO summits is probably of more interest here.
A first step into that direction came probably 2003 in the treaties to the second enlargement of NATO after the German reunification, i.e. the new membership of Bulgaria, Estonia, Latvia, Lithuania, Romania, Slovakia, and Slovenia. Here, for the first time new members committed to the 2 percent goal, as we can see from the following report from Colin Powell to the Congress:
They are prepared to shoulder their fair share of the burdens of our collective security.
They have all committed to spend a minimum of two percent of
their GDP on defense, higher than that of many current allies.
According to the following article at the Carnegie Europe think-tank, the decision to implement a common goal was made at the 2004 Istanbul NATO summit. In the so-called Istanbul Summit Communiqué issued by the Heads of State, we have the following paragraph:
We have invited the Secretary General and the Council in Permanent Session to take the steps necessary to ensure that the transformation process, including on questions of management and funding, is fully implemented.
The article then goes on to explain how the number of 2% was found:
At around the same time, staff at NATO headquarters noted that the median defense spending for 1991–2003 was 2 percent of GDP—that is, half of the allies spent more than 2 percent, and half spent less. When in 2006 NATO defense ministers approved the 2 percent guideline for the first time, their aim was twofold: to encourage the laggard half of the alliance to improve, and to develop the indicators pinpointed by leaders two years earlier.
The article refers probably to this report, where it is also stated, that there is a special NATO definition on defence expenditures, which could also deviate quite from the national expenditures.
So this goal was then institutionalized in 2006. However, the best I could find to corroborate this is the following NATO overview article, with the following paragraph:
In 2006, NATO member countries agreed to commit a minimum of two per cent of their Gross Domestic Product (GDP) to spending on defence. This guideline principally served as an indicator of a country’s political will to contribute to the Alliance’s common defence efforts.