Magento 2 how to get order collection

You can used this code in your custom module.

<?php
namespace 'YOUR_CUSTOM_NAME_SPACE';

class YOURCALSS extends \Magento\Framework\App\Action\Action
{

    protected $_orderCollectionFactory;

    public function __construct(
        Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory 
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        parent::__construct($context);

    }

    }
   public function YOURFUNCTION()
   {
       $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*')->addFieldToFilter($field, $condition);
    }


   }       
}

Refer the following function:

Class : Magento\Sales\Block\Order\History

 /**
     * @return bool|\Magento\Sales\Model\ResourceModel\Order\Collection
     */
    public function getOrders()
    {
        if (!($customerId = $this->_customerSession->getCustomerId())) {
            return false;
        }
        if (!$this->orders) {
            $this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
                '*'
            )->addFieldToFilter(
                'status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )->setOrder(
                'created_at',
                'desc'
            );
        }
        return $this->orders;
    }

If have any issues. Please comment. I will try to resolve.


Note: When I first came across this question, I was just beginning and I couldn't understand how to use the above answers in my code. That's why I'm writing this answer as a reference to any beginner who comes across this question like me.

Here is how I managed to get all orders in the past 3 days that have their statuses as 'pending'

<?php
namespace <Your Namespace Here>;
use \Psr\Log\LoggerInterface;

class Test {
    protected $logger;
    protected $orderRepository;
    protected $searchCriteriaBuilder;


    public function __construct(
        LoggerInterface $logger,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->logger = $logger;
        $this->orderRepository = $orderRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    public function execute() {
        $date = (new \DateTime())->modify('-3 day');

        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter(
                'status',
                'pending',
                'eq'
            )->addFilter(
                'created_at',
                $date->format('Y-m-d'),
                'gt'
            )->create();

        $orders = $this->orderRepository->getList($searchCriteria);
        foreach ($orders->getItems() as $order) {
            //Your Code Here
            $this->logger->info("Order# {$order->getIncrementId()} - Creation Date: {$order->getCreatedAt()}");
        }
    }

}