Edit: It's now easier since 2.8 as you can enable the Affect Only > Origins in the Options and use it to realign to a custom orientation.
If you've rotated your object in Edit mode, you will only be able to get back to the original rotation position with a trick like the one I'll explain below. In Object mode, if you reset the rotation to zero with alt R it will only reset the rotations you've made in Object mode, not the one you've made in Edit mode. And also, if you've applied the rotation in Object mode with ctrl A, you also won't be able to reset (again, except with the trick like the one below).
How to avoid it: if you want your object (cube for example) to remain orthogonal to the grid, don't rotate it in Edit mode, only rotate it in Object mode, and don't apply the rotation with ctrl A. This way you'll always be able to go back to the original position with a simple alt R.
Now, the trick to align a randomly rotated object (cube for example) to the grid
as explained by refa42 in a youtube video: https://www.youtube.com/watch?v=0pfTDzAaXp4