Rather than me explaining it here, the rails guides[1] give you a good overview.
[1] http://guides.rubyonrails.org/active_record_querying.html