One of the ways (there are several) by which the kidney excretes acid (and conserves sodium) is through generation of ammonia (and ammonium) from glutamine in the proximal convoluted tubules (and to a lesser extent absorption of ammonia from blood in the distal tubules). In the proximal convoluted tubule, the generated ammonium is excreted into the tubular lumen (in exchange for filtered sodium, which is not shown), where it combines with filtered chloride (not shown) to form ammonium chloride (which is then excreted). In this process, three bicarbonates are generated within the proximal tubular cell, which are then secreted into plasma along with the sodium (to maintain electroneutrality) with the NaHCO3 basolateral transporter. In this way, the kidney retains sodium, excretes chloride and generates new bicarbonate (x3), and gets rid of acid (as ammonium chloride). This is one way how the kidney compensates for a primary respiratory acidosis (reclaims bicarbonate and excretes chloride, resulting in a secondary metabolic alkalosis). The reverse occurs in a primary respiratory alkalosis, in which bicarbonate retention (and acid excretion) are reduced by decreased ammonia generation in the proximal tubules.
In the distal tubules, the kidney excretes additional acid as follows: Ammonia is absorbed from blood into the cell and is secreted into the tubular lumen, where it combines with hydrogen (generated within the tubular cell from water and carbon dioxide) which is actively pumped into the renal tubular lumen by a luminal H+ATPase. The single bicarbonate that is generated from carbonic anhydrase then exits the cell into blood in exchange for chloride (bicarbonate/chloride cotransporter), which passively follows H+ out (not shown), maintaining electroneutrality. Thus, the second way in which the kidney can also get rid of hydrogen (and retain bicarbonate) as a response to a primary respiratory acidosis is to promote the activity of the H+ATPase in the distal tubules, although this is less efficient than ammonia generation by the proximal tubules (which generates three versus one bicarbonate for blood). Indeed, in a primary respiratory acidosis, the H+ATPase is stimulated by pCO2, which is increased in blood and readily crosses the cell membrane from blood into the renal tubular cell (thus driving the reaction above). Note that aldosterone stimulates H+ATPase activity, promoting acid excretion in the distal tubules.