Excess nitrogen must be excreted by the body and can come from nitrogen in the diet (e.g. amino groups from excess amino acids) or nitrogen-containing compounds produced by bacteria in the gastrointestinal tract (GI tract).
Normally the liver and kidneys are involved in excreting ammonia ($NH_3$) and ammonium ($NH_4^+$) from the body. Ammonia can be toxic in high concentrations, but the body can detoxify it through conversion to other nitrogen-containing substances (i.e. urea) or excrete the ammonia itself.
Ammonia in the blood is freely filtered at the kidney glomerulus and can be "trapped" in the urine flow by accepting a proton to become ammonium (i.e. $H^+ + NH_3 = NH_4^+$) . Because of the positive charge the ammonium ion is unable to passively diffuse back across the tubules into the blood. This is one mechanism that ammonia is excreted. In the liver, the Urea Cycle (see any basic or clinical biochemistry text for more information) is a set of enzymes that converts most of the excess nitrogen in the form of ammonia to urea, which is a non-toxic, water soluble organic compound that can also be excreted in the urine. The body uses these processes to dispose of excess nitrogen from amino acids as well as ammonia that is produced as a by-product of bacterial metabolism in the GI tract.
Some patients with significant liver disease (e.g. cirrhosis from either NAFLD, hepatitis or any other liver-damaging disease) have lost the ability to convert all the excess ammonia to urea. In these patients, an easy way to prevent excessive nitrogen uptake in the gastrointestinal tract is by treating them with lactulose. Lactulose is a carbohydrate that that is not digestible or absorbable by the body, but is osmotically active (pulls water into the intestinal lumen) and acts to speed up intestinal transit. Since lactulose works by speeding up GI transit it makes you stool more frequently (which is why it is very useful for constipation). For liver disease patients, however, the lactulose acidifies (i.e. decreases pH) the lumen of the intestines and ammonia is protonated to form ammonium, which effectively traps it in the intestinal lumen. A lot of the excess ammonium can then pass into the feces and decrease the amount of ammonia that the liver needs to process. This is similar to the mechanism of ammonium in the urine as mentioned above. Additionally, lactulose also decreases the amount of GI bugs that produce ammonia containing compounds as well, which also lessens the amount of ammonia for detoxification by the liver. (Lactulose can also be used for a treatment for small intestinal overgrowth). These GI bacteria metabolize dietary amino acids and other compounds in the gut for energy metabolism, which liberates nitrogen-containing compounds that can be absorbed by the intestines.