The Q-code is a brevity code.
In the early days of radio, all communication was done by radiotelegraphy - in other words, the system would transmit simple tones, and that was all, so it was used as a 'wireless telegraph'. Naturally, radio operators used Morse code. Since Morse is fairly slow, and since the number of users of standard frequencies started to rise quite a bit as radio equipment became more common, operators began to look for a way to speed up communications. The other, more important reason for the codes was that they could be standardized regardless of the language spoken by the operator, which would sometimes be impossible (or at least very difficult) to even ascertain over radio.
At the Radiotelegraph Convention, held in London in 1912, a system was proposed and adopted. In this system, a standard set of three-character sequences, all beginning with the letter Q, were set out that had agreed-upon meanings. Q was used as it was a relatively rare opener (dah dah dit dah). These codes were substituted for the most common queries, responses and assertions that the operators were familiar with. Initially, this set of codes was meant for use by ships at sea, who were the most frequent users of radiotelegraph between unfamiliar operators (who didn't know each other's 'fist' well).
Eventually, aviation got into the game, and the Q-Codes were organized by service. The groups QAA through QNZ were reserved for aviation use; QOA through QQZ for maritime use, and QRA through QUZ to be shared by all operators.
The codes serve as both queries and answers, depending on context, and additional data is usually appended to the code. For example, "QSA" alone is a query, meaning 'What is my signal strength?' A response might read "QSA 5", or 'I am receiving you strength 5.' Note that the use of station identifiers (call letters) would indicate both who the sender was and who the intended recipient was.
The Q Codes have been removed from ICAO official use, and are falling out of favor as the use of radiotelegraphy fades. Some of them survive in other forms, for example 'QNH' is used in some countries to mean 'current atmospheric pressure' as the original meaning of that code was "What should I set on the subscale of my altimeter so that the instrument would indicate its elevation if my aircraft were on the ground at your station?"
Various military and police forces had their own versions of the Q-Codes, some of which were secret but most of which weren't, but which didn't always agree with the international set. A range was set aside for 'Other Use' - QZA-QZZ - and the more polite of them used that range for private signals.