I will be buying an expensive item in Germany, then after a few weeks I will visit Switzerland. A few days later I will take a direct flight home. How should I apply for my tax refund? Are there border agents on the border (of Basel) who can give me the refund? Does it have to be on the last leg of my trip (ie. flight home from Switzerland)?
The tax refund is available after the item is exported from the European Union, which happens when you take it to Switzerland. You do not get the tax from border agents but from the seller of the item. The agents merely stamp the export documentation to certify that the item was exported from the EU and that the VAT is therefore refundable.
Once you have that stamp, you have to return the documents to the seller, who can then refund the tax. Since many tourists are not in a position to do this, there are companies that take care of it for you in exchange for a significant commission.
The German customs service has a page detailing the process.
There are customs inspection posts on the Swiss/German border. You can find EU customs posts at https://ec.europa.eu/taxation_customs/dds2/rd/rd_consultation_location.jsp