The key to making a profit is to whom the money is distributed. In a for-profit business that money would be either saved for future use, invested in growing the business (research and development, acquisitions, facility/equipment, employees), or distributed to shareholders/owners. As for-profits invest in their business the goal is to increase the value of the business so that the shareholders/owners earn money on their investment.
In a nonprofit there are no ‘owners’. While you could figure out the value of a nonprofit it would only be for the exercise. Any excess funds in a nonprofit at the end of the year are assigned to future use, or invested for expanding their programing. There is a myth that nonprofits cannot have money available at the end of the year. The reality is that all capital must be allocated – even if just to a general fund. Having excess at the end of the year does not increase the wealth of any owners or shareholders – because by definition a nonprofit is not owned by anyone – but is owned by the public at large.
So yes, nonprofits can end the year with a profit. However that profit does not increase the wealth of individuals like in a for-profit.