I have the same problem with unconfirmed transaction and I already know how to solve it. But I don't know how to define transaction fee so that it doesn't happen again. I'm using the Bitcoin Core wallet and I chose the recommended option for transaction and fastest confirmation time. When I sent the transaction, a lower fee was applied than recommended and now the status of transaction is: 0/unconfirmed, in memory pool, broadcast through 1 node(s).
Bitcoin Core has updated their fee estimation logic several times in the past few years, another patch coming up shortly in Bitcoin Core 0.15.0. If you aren't running a current version of Bitcoin Core, you should first update your software to the most recent version.
If the problem occurred with a recent version of Bitcoin Core, you should consider checking different resources on the web to get a feeling for the current fee level on the network in order to sanity-check the fee level suggested by Bitcoin Core during the transaction generation process.
Some such sites are:
Another option would be to use the RBF feature, although this is currently only available for crafting transactions via commandline. A pullrequest to support it in the GUI is in the makings, I think that Bitcoin Knots (Luke Dashjr's client) already supports it in the GUI.