The function of the kidney is osmoregulation (the concentration of H2O in the body), ionoregulation (the concentration of specific ions in the body), and pH balance (concentration of H+ in the body). These functions are carried out by specifically transporting substances from the kidney tubule (inside the nephron) across the kidney epithilium (cells making up the nephron) and into the interstitial fluid, which surrounds the part of the epithelium opposite the side facing the lumen, where solutes are then taken up by the capillary network (vasa recta) surrounding the loop of henle.
There is a quantitatively unimportant but qualitatively important process called secretion whereby specific solutes, protons, or bicarbonate are moved from the interstitial fluid to the nephron lumen to be excreted. The later two processes are for acid base balance. The main ion secreted is K+. This is because all cells in the body maintain an electochemical gradient across their plasma membranes by use of the Na+/K+ ATPase to maintain a high concentration of K+ inside of the cells and high Na+ concentration outside of the cells. This function is crucial to the function of neurons and if the K+ concentration is too high outside of cells this function can be impared.
You should commit to memory that the only part of the kidney that actually acts to control the amounts (osmoregulation) of ions in the body is the collecting duct which will modify the amount of water excreted in the urine. All other parts of the kidney, proximal convoluted tubule, loop of henle, and distal convoluted tubule function the same, because their purpose is to generate an increasing concentration gradient from the cortex to the medulla regardless of whether the body is superhydrated, well hydrated, or dehydrated. However, the amount of either water or salt reabsorbed by the collecting will change depending on these states of water balance. When dehydrated the body will release anti-diuretic hormone (ADH) to increase the amount of water reaborbed by the collecting duct, thereby diluting the body fluids. When the body is superhydrated the ADH will not be released and the body will excrete a very diluted urine because the collecting duct is impermeable to water and since no ADH hormone has been released to increase water permeability the urine will remain dilute.
Now lets go over the function of the different parts of the nephron and how they contribute to a dilute urine if no ADH is around and a concentrated urine if ADH is around. The plasma being filtered into the proximal convoluted tubule is isoosmotic to plasma because no mechanism has yet acted upon that fluid to change its composition. The proximal convoluted tubules function is bulk reabsortion and therefore changes the amount of fluid in the proximal convoluted tubule but does not change its compositions and therefore the fluid remains isoosmotic. Since the decending limb of the loop of henle is permeable to both salt and water the osmotic pressure (amount of solutes dissolved in water without regard to chemical species/ whereas molarity pays attention the amount of solute disolved in water with regard to chemical species) between the loop and medulla will be equal and therefore isoosmotic. And since the concentration gradient increases from the cortex to the medulla the osmotic pressure will be greater here than in the proximal tubule. How does this concentration gradient arise? We will get to that when we discuss the thick ascending limb and the distal tubule. As the isoosmotic, concentrated solution enters the lowest part of the loop of henle it will be in the most concentrated part of the medulla. Now it enters the ascending limb and remains isoosmotic because this part is also permeable to both salt and water but because the concentration gradient now decreases from the medulla to the cortex the fluid in the ascending limb also becomes less concentrated. Now the fluid enters the thick ascending limb ( or diluting portion of the tubule) which is impermeable to both water and salt but actively pumps salt from the tubule lumen across the epithelial cells and into the interstitial fluid. Since this part removes salt and not water the fluid in this portion of the tubule becomes diluted. And since salt is moved into the interstitial fluid without water the interstitial fluid becomes concentrated. Now do you see how a concentration gradient is formed? This salt is then used to keep the concentration of fluids high in the medulla and low in the cortex by being transported there by the vasa recta which flows opposite to the direction of fluid flow in the tubule. Therefore when a concentrated fluid is moved into the vasa recta at the cortex it is carried away from there, effectively decreasing the concentration of fluids there, and moved to the medulla where an equilibrium is allowed to be established between the nephron there (descending loop) and vasa recta. Amazing! Now since both the ascending limb and distal tubule function the same we can see how a dilute urine will be excreted if no ADH is around because both of these parts of the nephron function to remove salt and retain water. So if no hormone (ADH is present) a very dilute urine will be removed from the body. And if ADH is present a concentrated urine will can be formed. Why? Think about how water would flow out at the cortex area if the collecting duct were permeable. It would equilibrate to the osmotic pressure outside. Since this portion has a low osmotic pressure only a small amout of water could flow out here. But, remember that the concentration gradient and therefore the osmotic pressure(gradient) increases from cortex to medulla so more water would be capable of flowing out as it passed down the nephron and into the ureter then bladder. Urea also acts to maintain this concentration gradient.
Urea is a way to excreted nitrogenous waste that can become toxic if is rises to high levels in the body. For example, it could cause proteins to come out of solution. Since only certain portions of the collecting duct and nephron are permeable to urea, urea can be recycled between these two structures. Let's see how this helps maintain the concentration gradient between the cortex and medulla. We know that there exists a concentration gradient between the cortex and medulla which is established and maintained by both the water impermeable ascending limb + distal tuble and vasa recta which is oriented in a countercurrent arrangement with the fluid flow through the nephron. This geometric relationship also allows urea recyclings and concentration in the meduall. We also know that chemicals will flow down there concentration gradient from a high to low concentration. Urea is no different. The amount (concentration) of urea in the blood is realtively low, because at high levels urea is toxic as mentioned, but as it flows through the nephron its concentration can become excedingly high as a result of selective urea permeablity. Indeed this selectivity helps to maintain the high medullary concentration gradient. Only the lowest portion of the loop of henle and lowest portion of the collecting duct are permeable to urea. And because all along the nephron the fluid is being modified and 99.9% of the intial filtered fluid is reabsorbed we can expect that at the end (collecting duct) only some water and solutes will be present but a lot of urea since most parts of the nephron are urea impermeable. Therefore, when we the fluid gets to the end of the collecting duct, which is urea permeable, urea flows down its concentration gradient from the collecting duct lumen into the surrounding interstial fluid, greatly increasing the concentration of solutes there and adding in the high medulla concentration. But the lowest part of the loop of henle is also permeable to urea and since the urea concentration is low in the lumen at this part since the blood concentration is low and much of the fluid still has not been reabsorbed the urea flows from the collecting duct to the loop of henle, cycling and thus maintaining a high medulla concentration gradient.
I know its a lot to read but it breaks kidney function down to the most fundemental mechanics by which it works. Hope that helps!!