I know medieval kingdoms and various nobles gain their wealth through owning estates or taxing peasants under their rule, or taxes indirectly obtained from their vassals, but where do they spend their money on? I assume there would be defense (procuring of arms, training retinues, constructing castles), infrastructure, churches, lavish living style etc. But in general, what is the most costly thing that medieval kings and nobles need to pay? Are there sources (maybe budget records)where I can look at to see how medieval nobles spent their money?
In terms of sources, this depends on the country. Records are preserved to different levels in different places, often depending on how often that nation has been invaded or suffered from destructive civil wars. Where they do survive, they are usually preserved in the records of the realm's financial officer - where I'm familiar with the sources, in Scotland and to a lesser extent England, this office was called the Exchequer. This answer will be based on my knowledge of medieval Scotland, so might not reflect the situation in other parts of medieval Europe.
The important thing to understand about medieval Scotland is that the royal administration didn't really have a huge pot of money to draw on. Unlike in England, where the monarchs had the power to levy huge taxes on the population (usually for fighting wars against France), the relative power of the Scottish nobility made large taxes politically infeasible for kings. A popular political theory in Scotland was that 'the king should live of his own', meaning that revenues from the royal estates, forfeited estates, and fees levied on imports should form the limit of the administration's expenditure. Kings who attempted to impose taxes to fund higher expenditure (notably David II and James I) found themselves very unpopular.
This restriction on the monarch's spending capability meant that most of the money in the royal coffers went to supporting the lifestyle of the king and his court. In Scotland, the royal court was expected to travel around the country fairly often, and this incurred extra expenses, particularly arranging for the adequate provision of food and wine for the the large royal entourage. Salaries would also need to be paid to royal officers great and small. The focus of royal policy could also result in other expenditures - a king interested in foreign affairs, like James I or James IV, would need to put money aside to finance royal embassies to France or the Papacy, while Alexander III, an expansionist king, needed to finance his purchase of the Western Isles from Norway.
But not all of the financial muscle of the crown was in direct money. The granting of royal offices to important nobles allowed them to keep a cut of the money generated by the state. The Justiciars, for example, who were the king's chief officers of justice, could keep part of the revenue they gained from legal proceedings (fines and forfeitures) for themselves, which gave them an investment in the government. The position of Justiciar was used by both Alexander II and Alexander III to retain the support of the powerful Comyn family. In effect, this was a bribe, but it would not show up in royal records as spending. In addition, as the coastal burghs grew in power and influence, powerful ports or merchant guilds were often granted exemptions from import/export levies, or cuts of the proceeds. Again, this doesn't show up as expenditure in the records, but was an important way of using financial means to retain political control as a king.
It might have surprised you that military expenditure hasn't really come up in this answer. Well, a lot of Scottish military expenditure was in effect outsourced by the crown. Kings usually financed the building of royal castles, but with a few exceptions they tended not to fund their upkeep. Favoured nobles would often be granted the custody of royal castles, which meant that they would pay for the garrison, food supplies, and general upkeep. This had the dual effect of saving the crown money, and giving the noble a platform for greater local influence, which meant that the custody of castles was also used to keep powerful nobles loyal. When it came to actually putting troops in the field, expenditure was generally shared between the king and the nobles (who had the responsibility for their own individual feudal following). Scotland's most successful military kings, particularly Robert I (the Bruce), used raids into English territory to keep their army in the field for longer, feeding off the land and thus saving valuable money.
That is a rough overview of the main categories of spending. Proportions, and how much was spent on what, varied wildly by king to king - based on the requirements of policy, how much money could actually be raised, and how much the king felt he could get away with keeping for himself.