Some routes will lose money, some will be cash-cows. The algorithm balances it all out for a net profit.
Enter an anti-algorithm that starts mucking with all of the carefully balanced numbers to do an optimization of its own and you end up with very unhappy airlines.
It's all a complicated algorithm but they have to provide reasonably reliable flights to all of the destinations that they serve in case someone gets bumped. (or they'll have to pay out huge fines to each passenger.)
What actual determines ticket price is mainly the supply and demand around trips between a pair of destinations (like with any other market). Lots of people want to move between DC and New York, and lots of them are business people who can afford to pay a lot, so the market can support a high price for this city pair. This isn't true of, say, DC and Yonkers. What's key, also, is that the layover cities (setting aside people doing the "hidden city" thing) are largely an implementation detail; if you're going from DC to Yonkers, you're comparing the flights that go through New York to flights that go through Philly directly on price (assuming about equal convenience), and you're going to pick the cheapest one. This means market forces are going to drive these prices to be similar, even if the DC->Philly market is radically different from the DC->NYC market. The consequence is weird, unintuitive situations where a flight to the intermediate city can be more expensive than one to the final destination, if market forces are such that the market can typically support a higher price for flights to that intermediate city than they can to the ultimate destination city. Hidden city travelers take advantage of the cost disparity between the markets -- it's a sort of arbitrage.