Is it always forbidden to swear to G-d, or does it depend on truth? I read somewhere that "in vain" means for something false or obvious. If someone doesn't believe me about something very important and asks me to swear to G-d, am I allowed?
The Seventh of the 613 Mitzvot according to the Rambam is:
So yes, there definitely are times to take oaths and there is even a mitzva to do so with God's name when one is taking an oath.
HOWEVER, I strongly caution against taking oaths unless absolutely neccasary as they are not undoable, and the punishment for breaking one is very strong. In the third of the Ten Commandments God himself says (immediately after the prohibition on idolatry) that he will not forgive one who takes his name in vain. So this is not something to be taken lightly.
Note that the Ramban in his critique of the Rambam's count argues that swearing with God's name when neccasary is not an obligatory act (חיוב) but rather an optional act (רשות). But even according to him it is permitted.
As someone who has studied Tractate Sh'vuos, there are actually situations where you must given testimony if you can.
(I will enlist some of the situations and the appropriate dafim in Sh'vuos as my source. These are undisputed other than some of the detail).
Although we still have Beis Din nowadays ruling in civil cases, they are not as authoritative as they used to be in generality, and therefore I do not think we still take these oaths, so someone with better knowledge will have to fill in the detail.
As @DoubleAA wrote, the Rambam seems to imply that there is a mitzva to swear in Hashem's name (when something is true).
However, there is a Medrash Tanchuma which says that since the verse says: "את ה' אלהיך תירא ואותו תעבוד ובו תדבק ובשמו תשבע" - "You will fear Hashem, serve him, cleave to him and swear in his name", one is only allowed to swear in Hashem's name if
(my question - how does point 2 and point 3 work together).
Moreover, the Midrash says that King Yanai had 2,000 villages destroyed because people swore truthfully.
Therefore, the Ramban interprets this verse to be a "Lav habah miklal asey" - a negative commandment that was said in a positive form (that one is prohibited to swear in the name of idols).
There is an argument as to the practical opinion of the Rambam - Does he follow his opinion in the Sefer Hamitzvos or does he say that it is just permissible to swear in Hashem's name (it implies in the Mishna Torah that one is permitted to swear truthfully.
The Minchas Yitzchak, however, explains that there is no contradiction between the Rambam and between the Rambam and the Braisa. He explains that there are three circumstances under which one can take an oath:
The Chasam Sofer says that one is forbidden to take an oath even if true, and his source is a verse in Koheles which says "All things come alike to all; there is one event to the righteous and to the wicked; to the good and to the clean and to the unclean; to him that sacrificeth and to him that sacrificeth not; as is the good, so is the sinner, and he that sweareth, as he that feareth an oath."
He asks what does the one who "swear"s and one who "fears an oath" swear about? If it was referring to a false oath, it would have been included in the previous line of "the good... the sinner". Rather, it must refer to the prohibition to swear truthfully.